Linux下调整根目录的空间大小

一、目的

在使用CentOS6.3版本linux系统的时候,发现根目录(/)的空间不是很充足,而其他目录空间有很大的空闲,所以本文主要是针对现在已有的空间进行调整。首先,先来查看一下系统的空间分配情况:

[root@CentOS-78 /]# df -h  
Filesystem            Size  Used Avail Use% Mounted on  
/dev/mapper/vg_centos-lv_root  
                       50G   14G   34G  30% /  
tmpfs                 1.9G     0  1.9G   0% /dev/shm  
/dev/sda1             485M   37M  423M   8% /boot  
/dev/mapper/vg_centos-lv_home  
                      404G  670M  382G   1% /home  

下面的详细步骤部分将从vg_centos-lv_home分区下取出100G的空间添加到/vg_centos-lv_root分区上去。

二、详细步骤

1、卸载vg_centos-lv_home分区

[root@CentOS-78 /]# umount /home

此时在用df命令查看将会发现/home目录已经看不见了,如下所示:

[root@CentOS-78 /]# df -h  
Filesystem            Size  Used Avail Use% Mounted on  
/dev/mapper/vg_centos-lv_root  
                       50G   14G   34G  30% /  
tmpfs                 1.9G     0  1.9G   0% /dev/shm  
/dev/sda1             485M   37M  423M   8% /boot 

2、从新设定vg_home-lv_home的大小

[root@CentOS-78 /]# resize2fs -p /dev/mapper/vg_centos-lv_home 282G  
resize2fs 1.41.12 (17-May-2010)  
Please run 'e2fsck -f /dev/mapper/vg_centos-lv_home' first.

这一步设定vg_home-lv_home大小没有成功,系统提示我们先运行下面的命令,操作如下:

[root@CentOS-78 /]# e2fsck -f /dev/mapper/vg_centos-lv_home  
e2fsck 1.41.12 (17-May-2010)  
Pass 1: Checking inodes, blocks, and sizes  
Pass 2: Checking directory structure  
Pass 3: Checking directory connectivity  
Pass 4: Checking reference counts  
Pass 5: Checking group summary information  
/dev/mapper/vg_centos-lv_home: 1386/26836992 files (0.9% non-contiguous), 1855856/107344896 blocks  

从新设定vg_home-lv_home的大小:

[root@CentOS-78 /]# resize2fs -p /dev/mapper/vg_centos-lv_home 282G  
resize2fs 1.41.12 (17-May-2010)  
Resizing the filesystem on /dev/mapper/vg_centos-lv_home to 73924608 (4k) blocks.  
Begin pass 2 (max = 43)  
Relocating blocks             XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX  
Begin pass 3 (max = 3276)  
Scanning inode table          XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX  
Begin pass 4 (max = 266)  
Updating inode references     XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX  
The filesystem on /dev/mapper/vg_centos-lv_home is now 73924608 blocks 

检查是否成功:

[root@CentOS-78 /]# mount /home  
[root@CentOS-78 /]#  
[root@CentOS-78 /]# df -h  
Filesystem            Size  Used Avail Use% Mounted on  
/dev/mapper/vg_centos-lv_root  
                       50G   14G   34G  30% /  
tmpfs                 1.9G     0  1.9G   0% /dev/shm  
/dev/sda1             485M   37M  423M   8% /boot  
/dev/mapper/vg_centos-lv_home  
                      278G  663M  263G   1% /home  
[root@CentOS-78 /]#  

3、将lv_home逻辑分区减小到指定大小

[root@CentOS-78 /]# lvreduce -L 282G /dev/mapper/vg_centos-lv_home  
  WARNING: Reducing active and open logical volume to 282.00 GiB  
  THIS MAY DESTROY YOUR DATA (filesystem etc.)  
Do you really want to reduce lv_home? [y/n]: y  
  Reducing logical volume lv_home to 282.00 GiB  
  Logical volume lv_home successfully resized  
[root@CentOS-78 /]#  

我认为这个命令是将已经减少的100G空间拿到系统公共的区域,可以被其他分区加载和利用。

查询卷组的信息:

[root@CentOS-78 /]# vgdisplay  
  --- Volume group ---  
  VG Name               vg_centos  
  System ID  
  Format                lvm2  
  Metadata Areas        1  
  Metadata Sequence No  5  
  VG Access             read/write  
  VG Status             resizable  
  MAX LV                0  
  Cur LV                3  
  Open LV               3  
  Max PV                0  
  Cur PV                1  
  Act PV                1  
  VG Size               465.27 GiB  
  PE Size               4.00 MiB  
  Total PE              119109  
  Alloc PE / Size       86472 / 337.78 GiB  
  Free  PE / Size       32637 / 127.49 GiB  
  VG UUID               1k4ooN-RFV9-uyf1-uMYf-aERG-YaGs-ZNoSD6  

Free PE / Size指定的应该是现在可在分配的空间。

4、增加vg_centos-lv_root分区的大小

将可用的空间添加到vg_centos-lv_root分区上面:

[root@CentOS-78 /]# lvextend -L +127.40G /dev/mapper/vg_centos-lv_root  
  Rounding up size to full physical extent 127.40 GiB  
  Extending logical volume lv_root to 177.40 GiB  
  Logical volume lv_root successfully resized  
[root@CentOS-78 /]#  

从新设定该分区的大小:

[root@CentOS-78 /]# resize2fs -p /dev/mapper/vg_centos-lv_root  
resize2fs 1.41.12 (17-May-2010)  
Filesystem at /dev/mapper/vg_centos-lv_root is mounted on /; on-line resizing required  
old desc_blocks = 4, new_desc_blocks = 12  
Performing an on-line resize of /dev/mapper/vg_centos-lv_root to 46504960 (4k) blocks.  
The filesystem on /dev/mapper/vg_centos-lv_root is now 46504960 blocks long.

5、再次查看分区大小

[root@CentOS-78 /]# df -h  
Filesystem            Size  Used Avail Use% Mounted on  
/dev/mapper/vg_centos-lv_root  
                      175G   14G  153G   9% /  
tmpfs                 1.9G     0  1.9G   0% /dev/shm  
/dev/sda1             485M   37M  423M   8% /boot  
/dev/mapper/vg_centos-lv_home  
                      278G  663M  263G   1% /home  

我们发现vg_centos-lv_root分区的空间已经增加了125G,之所以比lv_home减少的空间要多25G主要是由于我们把系统所有的可用的空间都加在了lv_root分区。

三、所遇到的问题

1、在卸载/home目录的时候失败

可先执行如下fuser命令,然后再umount即可:

[root@CentOS-78 /]# fuser -m /home  
[root@CentOS-78 /]#  

2、设定完lv_home的大小,再次mount该分区时,发现用df命令无法看到给分区,此时只要在mount一次即可

3、在设定lv_root的大小时,不要把Free PE / Size的空间全部都用上,这很可能会出现Free PE空间不足的现象,建议保留一点Free PE的空间。
==================================================================

参考:

http://hi.baidu.com/%B7%E7%B6%C0/blog/item/c4aa9a81b204dbc89023d956.html

http://qianxunniao.iteye.com/blog/1393726

NGiNX 转发到 Apache

序言

NGiNX 做前端反向代理处理静态请求,并发能力强,稳定,且占用内存少。 Apache 做后端 WEB 服务处理动态请求,高效,稳定,所以二者的搭配恰到好处。

  • CentOS 6.4 x86_64
  • nginx 1.4.1
  • Apache 2.2.15

配置 NGiNX 转发

# vi /etc/nginx/nginx.conf

添加以下内容;

# NGiNX 负载均衡
upstream backend {
    server 127.0.0.1:81 weight=1 fail_timeout=30s;
    server 127.0.0.1:82 weight=1 fail_timeout=30s;
    server 127.0.0.1:83 weight=1 fail_timeout=30s;
}

server {
    # 监听 80
    listen       80;
    server_name  test.aboutc.net;

    access_log  /var/log/nginx/aboutc-test.access.log  main;

    # 静态文件访问目录
    root   /var/www/html/test;
    index  index.html index.php;

    # 监听 ".php" 文件
    location ~ \.php$ {
        # 转发
        proxy_pass          http://backend;
        proxy_redirect      off;
        proxy_set_header    Host             $host;
        # 获取真实 IP
        proxy_set_header    X-Real-IP        $remote_addr;
        proxy_set_header    X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_set_header    X-Client-Verify  SUCCESS;
        proxy_set_header    X-SSL-Subject    $ssl_client_s_dn;
        proxy_set_header    X-SSL-Issuer     $ssl_client_i_dn;
        proxy_read_timeout  65;
    }
}

首先 NGiNX 要监听 80 端口成为前端代理的身份, 其次要配置 upstream 通过 proxy_pass 转发到后端 Apache 的监听地址, 完成动态请求的处理。这里的 NGiNX 只需要转发 .php 文件即可,它自身可以 直接获取 js/css 等静态文件。

配置 Apache VirtualHost

# vi /etc/httpd/conf.d/vhost.conf

注释掉 NameVirtualHost:

#NameVirtualHost *:80

并添加以下内容

Listen 81
Listen 82
Listen 83

<VirtualHost *:81>
    DocumentRoot /var/www/html/test
    ServerName test.aboutc.net
    ErrorLog logs/test81-error_log
    CustomLog logs/test81-access_log common
</VirtualHost>
<VirtualHost *:82>
    DocumentRoot /var/www/html/test
    ServerName test.aboutc.net
    ErrorLog logs/test82-error_log
    CustomLog logs/test82-access_log common
</VirtualHost>
<VirtualHost *:83>
    DocumentRoot /var/www/html/test
    ServerName test.aboutc.net
    ErrorLog logs/test83-error_log
    CustomLog logs/test83-access_log common
</VirtualHost>

/etc/httpd/conf.d/vhost.conf 文件是 Apache 配置文件 /etc/httpd/conf/httpd.conf 通过 Include conf.d/*.conf 引入的。请根据实际环境配置相应文件。

重启 NGiNX 和 Apache 服务使之生效

重新加载 NGiNX 配置:

# service nginx reload

重启 Apache 服务:

# service httpd start
(13)Permission denied: make_sock: could not bind to address [::]:81
(13)Permission denied: make_sock: could not bind to address 0.0.0.0:81
no listening sockets available, shutting down
Unable to open logs
                                                           [FAILED]

此问题是由于开启 SELINUX 引起的,关闭 SELINUX:

# setenforce 0

此方法是临时关闭 SELINUX,如果想永久关闭 SELINUX,编辑 /etc/selinux/config, 将SELINUX=enforcing 改为 SELINUX=disabled,重启系统。

再次重启 Apache:

# service httpd restart
Stopping httpd:                                            [  OK  ]
Starting httpd:                                            [  OK  ]

测试是否转发成功

  1. 添加 test.aboutc.net 到 /etc/hosts,以便我们可以直接在本地访问测试:

    vi /etc/hosts

添加以下内容:

127.0.0.1    test.aboutc.net
  1. 编辑测试文件

    $ vi /var/www/html/test/index.php

添加以下内容:

<?php
phpinfo();
?>
  1. 打开浏览器,访问测试地址 http://test.aboutc.net/,如下图

NGiNX转发到Apache

转自:http://blog.aboutc.net/nginx/40/nginx-forwarded-to-apache