Home | Mirror | Search | ITEYE 博客 | OSChina 博客 | 51CTO 博客

CentOS 6.4 + nginx-1.2.5 + php-5.4.15 + MySQL-5.5.31

redis-2.4.10

Mr. Neo Chen (netkiller), 陳景峰(BG7NYT)


中國廣東省深圳市龍華新區民治街道溪山美地
518131
+86 13113668890
+86 755 29812080

$Date: 2013-05-31 19:25:25 +0800 (Fri, 31 May 2013) $

摘要

在工作中,需要經常為新系統安裝軟件,重複而簡單,但又不得不作,我將過去幾年中工作中臨時寫的腳本這裡了一下,能夠實現半自動化安裝標本,只需要Ctrl+C, Ctrl+V 快速粘貼複製,即可快速完成安裝


目錄

1. CentOS 6.3 64bit Minior 安裝後 新機初始化常用軟件包安裝
1.1. /etc/resolv.conf
1.2. /etc/security/limits.conf
1.3. /etc/sysctl.conf
1.4. history 格式設置
1.5. /etc/fstab
2. 常用網絡軟件服務安裝
2.1. ntp
2.2. net-snmp
2.3. nagios
2.4. rsync
2.5. vsftpd
3. MySQL-5.5.31
4. php-5.4.15
4.1. 配置php.ini
4.2. 配置 php-fpm
4.3. 將php加入PATH環境變數
4.4. PHP Extension - APC
4.5. phpredis
4.6. mongo
4.7. 編譯中遇到問題如何處理?
5. nginx-1.2.5
5.1. /etc/nginx/nginx.conf
5.2. host 配置
6. redis-2.4.10
7. MongoDB

1. CentOS 6.3 64bit Minior 安裝後 新機初始化常用軟件包安裝

		
lokkit --disabled --selinux=disabled

yum update -y
rpm --import http://apt.sw.be/RPM-GPG-KEY.dag.txt
rpm -K http://packages.sw.be/rpmforge-release/rpmforge-release-0.5.2-2.el6.rf.x86_64.rpm
rpm -i http://packages.sw.be/rpmforge-release/rpmforge-release-0.5.2-2.el6.rf.x86_64.rpm

yum install -y telnet wget rsync
yum install -y system-config-network-tui
yum install -y bind-utils
yum install -y vim-enhanced

yum install -y openssh-clients
yum remove dhclient -y
		
		

1.1. /etc/resolv.conf

配置DNS

			
echo -ne "
search example.com
nameserver 208.67.222.222
nameserver 202.67.220.220
nameserver 8.8.8.8
nameserver 4.4.4.4
" > /etc/resolv.conf
			
			

1.2. /etc/security/limits.conf

修改檔案打開數

			
cat >> /etc/security/limits.conf <<EOF
root 	soft nofile 65536
root 	hard nofile 65536
www	soft nofile 65536
www 	hard nofile 65536
mysql	soft nofile 65536
mysql 	hard nofile 65536
EOF
			
			

下面更省事,但不建議使用*通配符,這樣任何用戶都不限制。

			
echo -ne "

* soft nofile 65536
* hard nofile 65536
" >> /etc/security/limits.conf
			
			

而上面我市針對www用戶設置,也就是web伺服器,其他用戶是受限的。

1.3. /etc/sysctl.conf

內核參數調整

			
cat >> /etc/sysctl.conf <<EOF

net.ipv4.ip_local_port_range = 1024 65500
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_fin_timeout = 60
net.ipv4.tcp_keepalive_time = 1200
net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.tcp_max_tw_buckets = 4096
EOF
			
			

1.4. history 格式設置

設置history格式,是他能夠記錄命令的輸入時間

			
cat >> /etc/bashrc <<EOF

export HISTTIMEFORMAT="%Y-%m-%d-%H:%M:%S "
EOF
			
			

1.5. /etc/fstab

我一般分區規劃是,/系統根分區,swap交換分區,/www數據分區,同時 禁止寫入atime時間,因為/www頻繁請求會影響IO

臨時mount

mount -o remount,noatime,nodiratime /dev/sda3 /mnt/your
			

LABEL 方式

LABEL=/www                 /www                       ext3    defaults,noatime,nodiratime        1 1
			

UUID 方式

UUID=eeff3e86-7964-4a48-ac02-51ea167ea6b2 /www        ext4    defaults,noatime,nodiratime        1 2
			

至此,Linux 的OS部分安裝配置與優化完成。

2. 常用網絡軟件服務安裝

2.1. ntp

ntp 服務同步伺服器的時間,需要安裝。

# redhat 5.6 cp /etc/ntp.conf.original /etc/ntp.conf
yum install ntp -y
			

指定ntp伺服器,如果你沒有ntp server,下面步驟不用執行

			
cp /etc/ntp.conf /etc/ntp.conf.original
vim /etc/ntp.conf <<VIM > /dev/null 2>&1
:22,24s/^/#/
:25,25s/^/\rserver 172.16.3.51\rserver 172.16.3.52\r/
:wq
VIM
			
			
service ntpd start
chkconfig ntpd on
			

2.2. net-snmp

用於伺服器的狀態監控

			
yum install net-snmp -y
vi /etc/snmp/snmpd.conf <<VIM > /dev/null 2>&1
:62,62s/systemview/all/
:85,85s/^#//
:wq
VIM
service snmpd start
chkconfig snmpd on
			
			

2.3. nagios

伺服器故障警告

			
yum install -y nrpe nagios-plugins
vi /etc/nagios/nrpe.cfg <<VIM > /dev/null 2>&1
:%s/allowed_hosts=127.0.0.1/allowed_hosts=172.16.1.2/
:wq
VIM

cat >> /etc/nagios/nrpe.cfg <<EOF

#command[check_http]=/usr/lib64/nagios/plugins/check_http -I 127.0.0.1 -p 80 -u http://www.example.com/index.html
command[check_swap]=/usr/lib64/nagios/plugins/check_swap -w 20% -c 10%
command[check_all_disks]=/usr/lib64/nagios/plugins/check_disk -w 20% -c 10% -e
EOF

chkconfig nrpe on
service nrpe start
			
			

2.4. rsync

我使用rsync來實現應用的部署。

			
yum install xinetd rsync -y

vim /etc/xinetd.d/rsync <<VIM > /dev/null 2>&1
:%s/yes/no/
:wq
VIM

cat > /etc/rsyncd.conf <<EOD
uid = root
gid = root
use chroot = no
max connections = 8
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log

hosts deny=*
hosts allow=192.168.2.0/255.255.255.0

[www]
    uid = www
    gid = www
    path = /www
    ignore errors
    read only = no
    list = no
    auth users = www
    secrets file = /etc/rsyncd.passwd
EOD

cat >> /etc/rsyncd.passwd <<EOF
www:xxxxxxxxxxxxxxxx
EOF

chmod 600 /etc/rsyncd.*
chmod 600 /etc/rsyncd.passwd

service xinetd restart
			
			

2.5. vsftpd

根據你的需要,選裝。

			
yum install -y vsftpd
adduser --home-dir /www/target/logs/ --shell /sbin/nologin --password logs.xiu.com logs
echo logs >> /etc/vsftpd/chroot_list

vim /etc/vsftpd/vsftpd.conf <<VIM > /dev/null 2>&1
:%s/#chroot_list_enable=YES/chroot_list_enable=YES/
:%s/#chroot_list_file/chroot_list_file/
VIM

chkconfig vsftpd on
service vsftpd start
			
			

3. MySQL-5.5.31

卸載舊的包,然後準備好rpm檔案

rpm -e --nodeps mysql-libs
yum localinstall MySQL-*
		

安裝後查看如下

# rpm -qa | grep MySQL
MySQL-client-5.5.31-1.el6.x86_64
MySQL-shared-5.5.31-1.el6.x86_64
MySQL-devel-5.5.31-1.el6.x86_64
MySQL-shared-compat-5.5.31-1.el6.x86_64
MySQL-server-5.5.31-1.el6.x86_64
		

4. php-5.4.15

由於redhat與centosYUM源中尚未提供php-fpm所以我採用編譯安裝,網上有一些愛好者製作過php-fpm的rpm包。是否使用你自己那注意。

安裝fedora的YUM源,我們需要一些devel包。

rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-7.noarch.rpm
		

安裝編譯器與開發包

yum install gcc gcc-c++ make automake autoconf -y
yum install curl-devel libmcrypt-devel gd-devel libjpeg-devel libpng-devel libXpm-devel libxml2-devel libxslt-devel mhash-devel recode-devel openssl-devel -y
		

安裝下面三個rpm包,編譯php不需要server與client兩個包

MySQL-shared-5.5.31-1.el6.x86_64
MySQL-devel-5.5.31-1.el6.x86_64
MySQL-shared-compat-5.5.31-1.el6.x86_64
		

將其放置在一個目錄中,使用下面命令即可安裝

yum localinstall MySQL-*
		

前面的準備工作就緒後,便可以進入到php的安裝階段

		
tar zxvf php-5.4.15.tar.gz
cd php-5.4.15

./configure --prefix=/srv/php-5.4.15 \
--with-config-file-path=/srv/php-5.4.15/etc \
--with-config-file-scan-dir=/srv/php-5.4.15/etc/conf.d \
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \
--with-curl \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-zlib-dir \
--with-iconv \
--with-mcrypt \
--with-mysql \
--with-mysqli=/usr/bin/mysql_config \
--with-pdo-mysql \
--with-mysql-sock=/var/lib/mysql/mysql.sock \
--with-openssl=shared \
--with-mhash=shared \
--with-xsl=shared \
--with-recode=shared \
--with-pear \
--enable-sockets \
--enable-soap \
--enable-mbstring \
--enable-gd-native-ttf \
--enable-zip \
--enable-xml \
--enable-bcmath \
--enable-calendar \
--enable-shmop \
--enable-dba \
--enable-wddx \
--disable-debug


make && make install
		
		

4.1. 配置php.ini

備份配置檔案,防止不小心改錯而又找不出問題出在哪裡。

mkdir -p /srv/php-5.4.15/etc/conf.d
cp php.ini-* /srv/php-5.4.15/etc/
cp /srv/php-5.4.15/etc/php.ini-development /srv/php-5.4.15/etc/php.ini
cp /srv/php-5.4.15/etc/pear.conf{,.original}
ln -s /srv/php-5.4.15/ /srv/php
			

php預設提供兩個配置檔案模板php.ini-development用於開發和測試環境;php.ini-production用於生產環境。他們差異是php.ini-development會顯示調試信息,即 error_display = On.

# ls php.ini-*
php.ini-development  php.ini-production
			

下面更改僅供參考,需要根據你的具體情況配置,不要照搬。

			
vim /srv/php-5.4.15/etc/php.ini <<EOF > /dev/null 2>&1
:%s#expose_php = On#expose_php = Off#
:%s$;open_basedir = $open_basedir = /www/:/tmp/:/srv/php-5.4.15/lib/php/:/srv/php-5.4.15/bin/$
:%s/memory_limit = 128M/memory_limit = 16M/
:%s!;include_path = ".:/php/includes"!include_path = ".:/srv/php-5.4.15/lib/php:/srv/php-5.4.15/share"!
:%s:extension_dir = "./":extension_dir = "/srv/php-5.4.15/lib/php/extensions":
:%s/upload_max_filesize = 2M/upload_max_filesize = 8M/
:%s/;cgi.fix_pathinfo=0/cgi.fix_pathinfo=1/
:%s$;date.timezone =$date.timezone = Asia/Hong_Kong$
:1367,1367s/session.save_handler = files/session.save_handler = redis/
:1368,1368s#^#session.save_path = "tcp://127.0.0.1:6379"\r#
:1415,1368s/session.name = PHPSESSID/session.name = JSESSIONID/
:wq
EOF
			
			

4.2. 配置 php-fpm

安裝啟動腳本

cp ./sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm

chkconfig --add php-fpm
chkconfig php-fpm on
chkconfig --list php-fpm
			

修改配置檔案

			
cp /srv/php-5.4.15/etc/php-fpm.conf.default /srv/php-5.4.15/etc/php-fpm.conf

vim /srv/php-5.4.15/etc/php-fpm.conf <<end > /dev/null 2>&1
:25,25s/;//
:32,32s/;//
:wq
end
			
			

察看配置項

# grep -v '^;' /srv/php-5.4.15/etc/php-fpm.conf | grep -v '^$'
[global]
pid = run/php-fpm.pid


[www]
user = www
group = www
listen = 127.0.0.1:9000

pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
			

4.3. 將php加入PATH環境變數

			
vim  ~/.bash_profile <<END > /dev/null 2>&1
:%s#bin#bin:/srv/php/bin#
:wq
END
			
			

4.4. PHP Extension - APC

Apc

			
pecl install apc

cat > /srv/php-5.4.15/etc/conf.d/apc.ini <<EOF
extension=apc.so
EOF
			
			

查看安裝情況

# /srv/php/bin/php -m | grep apc
apc
			

4.5. phpredis

安裝git版本控制客戶端

yum install git
			

從github倉庫中克隆一份代碼到本地

git clone git://github.com/nicolasff/phpredis.git
			

編譯安裝phpredis; 我暫時沒有找到 pecl的phpredis源

			
cd phpredis
phpize
./configure --with-php-config=/srv/php-5.4.15/bin/php-config
make && make install
			
			

創建配置檔案

			
cat > /srv/php-5.4.15/etc/conf.d/redis.ini <<EOF
extension=redis.so
EOF
			
			

查看安裝情況

# php -m | grep redis
redis
			

4.6. mongo

pecl 安裝 mongo

pecl install mongo
			

創建配置檔案

			
cat > /srv/php-5.4.15/etc/conf.d/mongo.ini <<EOF
extension=mongo.so
EOF
			
			
# php -m | grep mongo
mongo
			

4.7. 編譯中遇到問題如何處理?

下面我們來模擬一個場景,例如 configure的時候出現下面提示

configure: error: Can not find recode.h anywhere under yes /usr/local /usr /opt.
			

提示找不到recode。h檔案,很好辦,首先搜索recode包

[root@haproxy php-5.4.15]# yum search recode                                                                                                                                                                                                                        |  327 B     00:00
============================ N/S Matched: recode ====================================
php-recode.x86_64 : A module for PHP applications for using the recode library
recode-devel.i686 : Header files and static libraries for development using recode
recode-devel.x86_64 : Header files and static libraries for development using recode
recode.i686 : Conversion between character sets and surfaces
recode.x86_64 : Conversion between character sets and surfaces

  Name and summary matches only, use "search all" for everything.
			

然後安裝devel包,記住之需要安裝devel包即可,其他都是多餘。

[root@haproxy php-5.4.15]# yum install recode-devel -y
			

然後重新運行configure

5. nginx-1.2.5

為web伺服器創建一個用戶,我喜歡使用www,id為80更容易記,同時將一個單獨分區掛在/www上用戶存放web應用程序。

groupadd -g 80 www
adduser -o --home /www --uid 80 --gid 80 -c "Web Application" www
		

添加nginx的YUM源

		
cat > /etc/yum.repos.d/nginx.repo <<EOF
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/6/x86_64/
gpgcheck=0
enabled=1
EOF
		
		

開始安裝nginx

		
yum search nginx

yum install -y nginx
chkconfig nginx on
service nginx start
		
		

如果你不懂編譯器優化,建議你使用rpm方案。在不優化的情況下編譯出來程序很臃腫。

# rpm -qa | grep nginx
nginx-1.2.5-1.el6.ngx.x86_64
		

5.1. /etc/nginx/nginx.conf

隱藏nginx版本號

vim /etc/nginx/nginx.conf

http {
...
    server_tokens off;
}
			

5.2. host 配置

mkdir -p /www/www.mydomain.com/htdocs

cd /etc/nginx/conf.d
cp default.conf www.mydomain.com.conf
vim www.mydomain.com.conf
			
server {
    listen       80;
    server_name  www.mydomain.com;

    charset utf-8;
    access_log  /var/log/nginx/www.mydomain.com.access.log  main;

    location / {
        root   /www/www.mydomain.com/htdocs;
        index  index.html index.php;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        root           html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /www/www.mydomain.com/htdocs$fastcgi_script_name;
        include        fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    location ~ /\.ht {
        deny  all;
    }
}
			

創建測試頁面

			
cat >> /www/www.mydomain.com/htdocs/index.php <<PHP
<?php
phpinfo();
PHP
			
			

啟動伺服器

service php-fpm start
service nginx start
			

檢查index.php輸出

# curl -H HOST:www.mydomain.com http://127.0.0.1/index.php
			

6. redis-2.4.10

安裝fedora的YUM源,

rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-7.noarch.rpm
		

安裝redis

# yum install redis

# chkconfig redis on

# service redis start
		

7. MongoDB

# yum install mongodb-server

# chkconfig mongod on

# service mongod start
		
comments powered by Disqus