一、系統約定
軟件源代碼包存放位置
/usr/local/src
源碼包編譯安裝位置(prefix)
/usr/local/software_name
腳本以及維護程序存放位置
/usr/local/sbin
MySQL 數據庫位置
/var/lib/mysql(可按情況設置)
Nginx 網站根目錄
/home/www/wwwroot(可按情況設置)
Nginx 虛擬主機日志根目錄
/home/www/logs(可按情況設置)
Nginx 運行賬戶
www:www
二、系統環境初始化
1. 檢查系統是否正常
# more /var/log/messages(檢查有無系統級錯誤信息)
# dmesg (檢查硬件設備是否有錯誤信息)
# cat /proc/cpuinfo (檢查CPU頻率是否正常)
# top (按1檢測CPU核數是否正常,內存大小是否正常)
# ifconfig(檢查網卡設置是否正確)
# ping www.163.com(檢查網絡是否正常)
2. 關閉不需要的服務
# ntsysv
以下僅列出需要啟動的服務,未列出的服務一律推薦關閉:
atd
crond
irqbalance
microcode_ctl
network
sendmail
sshd
syslog
關閉SElinux:修改/etc/selinux/config文件中的SELINUX= 為 disabled
3.更換快速源
# cd /etc/yum.repos.d/
# mv CentOS-Base.repo CentOS-Base.repo.old
# wget http://centos.ustc.edu.cn/CentOS-Base.repo.5
# mv CentOS-Base.repo.5 CentOS-Base.repo
# yum update
4. 使用 yum 程序安裝所需開發包(以下為標准的 RPM 包名稱)
# yum -y install ntp make openssl openssl-devel pcre pcre-devel libpng libpng-devel libjpeg-6b libjpeg-devel-6b freetype freetype-devel gd gd-devel zlib zlib-devel gcc gcc-c++ libXpm libXpm-devel ncurses ncurses-devel libmcrypt libmcrypt-devel libxml2 libxml2-devel imake autoconf automake screen sysstat compat-libstdc++-33 curl curl-devel
5. 定時校正服務器時鐘,定時與中國國家授時中心授時服務器同步
# crontab -e
加入一行:
*/30 * * * * ntpdate 210.72.145.44 > /dev/null 2>&1
6. 下載程序包
請到各程序官方網站下載最新的穩定版本存放到/usr/local/src/目錄
Nginx
當前穩定版0.7.65http://wiki.nginx.org/NginxChs
PHP
當前穩定版5.2.13http://cn.php.net/releases/
PHP FPM補丁 當前穩定版0.5.14
http://php-fpm.org/downloads/
Mysql
當前穩定版5.0.86http://dev.mysql.coml/
Zend
當前穩定版3.3.3http://www.zend.com/
Memcache
當前穩定版2.2.5http://pecl.php.net/package/memcache/
Memcached
當前穩定版1.4.5
http://memcached.googlecode.com
Libiconv
當前穩定版1.13.1
http://www.gnu.org/software/libiconv/
libevent
當前穩定版1.4.13
http://www.monkey.org/~provos/libevent/
文中的配置文件和腳本限於篇幅,請到以下地址下載
Nginx控制腳本
http://c.sihost.net/jb/nginx
PHP FPM配置文件
http://c.sihost.net/jb/php-fpm.conf
Nginx 配置文件
http://c.sihost.net/jb/nginx.conf
FCGI配置文件
http://c.sihost.net/jb/fcgi.conf
以上列出的程序最新穩定版下載列表
http://c.sihost.net/lnmp_tools.list
在 /usr/local/src目錄下執行
wget http://c.sihost.net/lnmp_tools.list
wget -i lnmp_tools.list
7. 創建web運行用戶
# groupadd www
# useradd -g www www
# mkdir -p /home/www/wwwroot
# chmod +w /home/www/wwwroot
# chown www:www /home/www/wwwroot -R
8. 重新啟動
# init 6
三、編譯安裝環境
1. 安裝MySQL
# cd /usr/local/src
# tar zxvf mysql-5.0.86-linux-i686-icc-glibc23.tar.gz
# mv mysql-5.0.86-linux-i686-icc-glibc23 /usr/local/
# ln -s /usr/local/mysql-5.0.86-linux-i686-icc-glibc23/ /usr/local/mysql
# groupadd mysql
# useradd -g mysql mysql -d /home/mysql -s /sbin/nologin
# chown -R mysql:mysql /usr/local/mysql
# chown -R mysql:mysql /usr/local/mysql-5.0.86-linux-i686-icc-glibc23/
# cd /usr/local/mysql
# ./scripts/mysql_install_db --user=mysql
# cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld
# chmod 755 /etc/rc.d/init.d/mysqld
# chkconfig --add mysqld
# chkconfig --level 3 mysqld on
# cp ./support-files/my-huge.cnf /etc/my.cnf
# mv /usr/local/mysql/data /var/lib/mysql
# chown -R mysql:mysql /var/lib/mysql
編輯/etc/my.cnf
在 [mysqld] 段增加
datadir = /var/lib/mysql
skip-innodb
wait-timeout = 10
max_connections = 512
max_connect_errors = 10000000
在 [mysqld] 段修改
max_allowed_packet = 16M
thread_cache_size = CPU個數*2
將 log-bin 注釋
service mysqld start
# bin/mysqladmin -u root password password
其中紅色的password是要設置的root密碼
2. 編譯安裝Nginx
# cd /usr/local/src/
# tar zxvf nginx-0.7.65.tar.gz
# cd nginx-0.7.65
# ./configure --prefix=/usr/local/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --with-http_realip_module --with-http_addition_module --with-http_gzip_static_module --with-http_random_index_module --with-http_stub_status_module --with-http_sub_module --with-http_dav_module
# make
# make install clean
# cp /usr/local/src/nginx /etc/init.d/nginx
# chmod 755 /etc/init.d/nginx
# chkconfig --add nginx
# chkconfig nginx on
3. 編譯安裝PHP 所需的支持庫
Libiconv
# cd /usr/local/src/
# tar zxvf libiconv-1.13.1.tar.gz
# cd libiconv-1.13.1/
# ./configure --prefix=/usr/local
# make
# make install
# cd ..
Libmcrypt
# tar zxvf libmcrypt-2.5.8.tar.gz
# cd libmcrypt-2.5.8
# ./configure
# make
# make install
# /sbin/ldconfig
# cd libltdl/
# ./configure --enable-ltdl-install
# make
# make install
# cd /usr/local/src/
Mhash
# tar zxvf mhash-0.9.9.9.tar.gz
# cd mhash-0.9.9.9/
# ./configure
# make
# make install
# cd ..
# ln -s /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la
$ ln -s /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so
$ ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4
$ ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/libmcrypt.so.4.4.8
# ln -s /usr/local/lib/libmhash.a /usr/lib/libmhash.a
# ln -s /usr/local/lib/libmhash.la /usr/lib/libmhash.la
# ln -s /usr/local/lib/libmhash.so /usr/lib/libmhash.so
# ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2
# ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1
$ ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config
# tar zxvf mcrypt-2.6.8.tar.gz
# cd mcrypt-2.6.8
# /sbin/ldconfig
# ./configure
# make
# make install
4. 編譯安裝PHP
# cd /usr/local/src
# tar zxvf php-5.2.13.tar.gz
# gzip -cd php-5.2.13-fpm-0.5.14.diff.gz | patch -d php-5.2.13 -p1
# cd php-5.2.13
# ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/etc/cgi --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-iconv-dir=/usr/local --with-freetype-dir=/usr --with-jpeg-dir=/usr --with-png-dir=/usr --with-zlib --with-libxml-dir=/usr --with-xmlrpc --enable-xml --enable-mbstring --enable-ftp --with-gd --enable-magic-quotes --with-pear --enable-sockets --with-ttf --enable-gd-native-ttf --enable-sysvsem --enable-sysvshm --enable-shmop --enable-force-cgi-redirect --enable-fastcgi --enable-zip --enable-fpm --with-mhash --with-mcrypt --enable-discard-path --enable-bcmath --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --with-openssl
# make ZEND_EXTRA_LIBS='-liconv'
# make install
# mkdir -p /usr/local/etc/cgi/
# cp php.ini-dist /usr/local/etc/cgi/php.ini
# mv -f /usr/local/src/php-fpm.conf /usr/local/php/etc/php-fpm.conf
# echo 'ulimit -SHn 65535' >> /etc/rc.local
# echo '/usr/local/php/sbin/php-fpm start' >> /etc/rc.local
5. 安裝PHP擴展模塊(需要此功能的Discuz! X用戶選裝)
Memcache
# cd /usr/local/src
# tar zxvf memcache-2.2.5.tgz
# cd memcache-2.2.5/
# /usr/local/php/bin/phpize
# ./configure --with-php-config=/usr/local/php/bin/php-config
# make
# make install
修改php.ini文件,路徑/usr/local/etc/cgi/
查找:extension_dir = "./"
修改成:extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/"
在此句下面一行
添加:extension = "memcache.so"
查找:output_buffering = Off
修改成:output_buffering = On
保存。
Memcached
# cd /usr/local/src
#tar zxvf libevent-1.4.13-stable.tar.gz
# cd libevent-1.4.13-stable
# ./configure --prefix=/usr/lib
# make
# make install
# cd ../
# tar zxvf memcached-1.4.5.tar.gz
# cd memcached-1.4.5
# ./configure --prefix=/usr/local --with-libevent=/usr/lib/
# make
# make install
# /usr/local/bin/memcached -d -u www -m 128
-p <num>
監聽的TCP端口(缺省: 11211)
-d
以守護進程方式運行 以守護進程方式運行 Memcached Memcached
-u <username>
運行運行 Memcached的賬戶 非root用戶
-m <num>
最大的內存使用單位是MB 缺省是64MB
-c <num>
軟連接數量缺省是1024
-v
輸出警告和錯誤信息
-vv
打印客戶端的請求和返回信息
-h
打印幫助信息
-i
打印memcached和libevent的版權信息
6. 安裝ZendOptimizer
# cd ..
# tar zxvf ZendOptimizer-3.3.3-linux-glibc23-i386.tar.gz
# cd ZendOptimizer-3.3.3-linux-glibc23-i386
# ./install
Please specify the location for installing
Zend Optimizer:
這裡填寫Zend的安裝路徑,按下面寫即可
/usr/local/Zend
Confirm the location of your
php.ini file:
這裡填寫的是php.ini的路徑,按下面寫即可
/usr/local/etc/cgi
Are you using Apache Web server?
這裡選No
7.啟動Nginx和PHP
# mv -f /usr/local/src/fcgi.conf /usr/local/nginx/conf/
# cp -f /usr/local/src/nginx.conf /usr/local/nginx/conf/nginx.conf
# ulimit -SHn 65535
# /usr/local/php/sbin/php-fpm start
# service nginx start
8.關於安全設置
查找/usr/local/etc/cgi/php.ini裡的
; cgi.fix_pathinfo=0
把這句前的分號去掉,變成
cgi.fix_pathinfo=0