编译安装Nginx + MySQL + PHP + PureFTP

* 本文约定

操作系统 Cent OS 5.x(32bit)
Nginx版本 0.8.52
PHP版本 5.2.13
MySQL版本 5.1.46
软件源代码包存放位置 /usr/local/src/lnmp
源码包编译安装位置(prefix) /usr/local/software_name
MySQL数据库data路径 /var/lib/mysql
网站文档父目录 /home/www/
Nginx和php-cgi运行用户 www:www

* 自动安装

您可以选择使用我们编写的安装脚本进行自动安装,自动安装和您手工按本文中所写的方法安装效果是一样的。
执行下面的2条命令即可完成自动安装。
安装完成后所有的帐号信息会保存在/root/setup.txt中,同时也会包含一些必要的帮助信息。

wget http://wiki.diahosting.com/down/lnmp/install.sh
chmod 700 install.sh
./install.sh

一,安装所需要的程序和库

yum install -y gcc gcc-c++ glibc glibc-devel glib2 glib2-devel unzip make ncurses ncurses-devel autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel bzip2 bzip2-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers xinetd patch

二,下载程序源码

mkdir -p /usr/local/src/lnmp
cd /usr/local/src/lnmp

wget http://wiki.diahosting.com/down/lnmp/libiconv-1.13.tar.gz
wget http://wiki.diahosting.com/down/lnmp/mhash-0.9.9.9.tar.gz
wget http://wiki.diahosting.com/down/lnmp/pcre-7.9.tar.gz
wget http://wiki.diahosting.com/down/lnmp/libmcrypt-2.5.8.tar.gz
wget http://wiki.diahosting.com/down/lnmp/mysql-5.1.46.tar.gz
wget http://wiki.diahosting.com/down/lnmp/php-5.2.13-fpm-0.5.14.diff.gz
wget http://wiki.diahosting.com/down/lnmp/mcrypt-2.6.8.tar.gz
wget http://wiki.diahosting.com/down/lnmp/nginx-0.8.52.tar.gz
wget http://wiki.diahosting.com/down/lnmp/php-5.2.13.tar.gz
wget http://wiki.diahosting.com/down/lnmp/ZendOptimizer-3.3.9-linux-glibc23-i386.tar.gz
wget http://wiki.diahosting.com/down/lnmp/eaccelerator-0.9.6.tar.gz
wget http://wiki.diahosting.com/down/lnmp/phpMyAdmin.tar.gz
wget http://wiki.diahosting.com/down/lnmp/pure-ftpd-1.0.29.tar.gz
wget http://wiki.diahosting.com/down/lnmp/php-fpm.conf
wget http://wiki.diahosting.com/down/lnmp/nginx.conf
wget http://wiki.diahosting.com/down/lnmp/phpMyAdmin.conf
wget http://wiki.diahosting.com/down/lnmp/default.conf
wget http://wiki.diahosting.com/down/lnmp/nginx.init

for FILE in ./*.tar.gz; do tar xvf $FILE; done

三,编译安装MySQL

(1)添加供mysqld运行使用的帐号
groupadd mysql
useradd -s /sbin/nologin -M -g mysql mysql
(2)配置编译参数
cd /usr/local/src/lnmp/mysql-5.1.46
./configure --prefix=/usr/local/mysql --enable-assembler --with-charset=utf8 --enable-thread-safe-client --with-extra-charsets=all --with-big-tables
(3)编译并安装
make;make install
(4)更改mysql程序目录属主
chown -R mysql:mysql /usr/local/mysql
(5)复制配置文件
cp support-files/my-medium.cnf /etc/my.cnf
(6)编辑配置文件/etc/my.cnf
[mysqld]
basedir=/usr/local/mysql
datadir=/var/lib/mysql
(7)创建用来管理访问权限的数据库mysql
/usr/local/mysql/bin/mysql_install_db --basedir=/usr/local/mysql --datadir=/var/lib/mysql --user=mysql
(8)复制mysqld管理脚本
cp /usr/local/mysql/share/mysql/mysql.server /etc/init.d/mysqld
chmod 755 /etc/init.d/mysqld
(9)启动mysqld
/etc/init.d/mysqld start
(10)给root设置一个密码

注意替换root-password为您自己的MySQL root密码。

/usr/local/mysql/bin/mysqladmin -u root password root-password
(11)设置MySQL在系统启动后自动运行
chkconfig mysqld on
(12)把常用的程序链接进PATH中
ln -s /usr/local/mysql/bin/myisamchk /usr/bin/
ln -s /usr/local/mysql/bin/mysql /usr/bin/
ln -s /usr/local/mysql/bin/mysqldump /usr/bin/

四,编译安装PHP

(1)安装PHP运行所需要的库程序
cd /usr/local/src/lnmp/libiconv-1.13/
./configure --prefix=/usr/local
make
make install

cd /usr/local/src/lnmp/libmcrypt-2.5.8/
./configure --exec-prefix=/usr
make
make install
/sbin/ldconfig
cd libltdl/
./configure --enable-ltdl-install
make
make install

cd /usr/local/src/lnmp/mhash-0.9.9.9/
./configure --prefix=/usr
make
make install

cd /usr/local/src/lnmp/mcrypt-2.6.8/
/sbin/ldconfig
./configure
make
make install
(2)安装PHP
cd /usr/local/src/lnmp/
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=/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-iconv-dir=/usr/local --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-discard-path --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fastcgi --enable-fpm --enable-force-cgi-redirect --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-ldap --with-ldap-sasl --with-xmlrpc --enable-zip --enable-soap --with-pear=/usr/local/php/pear
make ZEND_EXTRA_LIBS='-liconv'
make install
cp php.ini-recommended /etc/php.ini
(3)修改PHP配置文件/etc/php.ini
sed -i 's#extension_dir = "./"#extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/"#' /etc/php.ini
sed -i 's/upload_max_filesize = 2M/upload_max_filesize = 32M/g' /etc/php.ini
sed -i 's/short_open_tag = Off/short_open_tag = On/g' /etc/php.ini
sed -i 's/; cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/g' /etc/php.ini
(4)安装Zend
cd /usr/local/src/lnmp/ZendOptimizer-3.3.9-linux-glibc23-i386/data/5_2_x_comp
mkdir -p /usr/local/ZendOptimizer/
cp ZendOptimizer.so /usr/local/ZendOptimizer/ZendOptimizer.so

在/etc/php.ini的末尾加入下面的内容

[Zend Optimizer] 
zend_optimizer.optimization_level=1 
zend_extension="/usr/local/ZendOptimizer/ZendOptimizer.so" 
(5)安装eAccelerator对PHP进行加速
cd /usr/local/src/lnmp/eaccelerator-0.9.6
/usr/local/php/bin/phpize
./configure --enable-eaccelerator=shared --with-php-config=/usr/local/php/bin/php-config
make
make install
mkdir /tmp/eaccelerator
chmod 0777 /tmp/eaccelerator

在/etc/php.ini的末尾加入下面的内容

[eAccelerator] 
extension="eaccelerator.so"
eaccelerator.shm_size="16"
eaccelerator.cache_dir="/tmp/eaccelerator"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
eaccelerator.filter=""
eaccelerator.shm_max="0"
eaccelerator.shm_ttl="0"
eaccelerator.shm_prune_period="0"
eaccelerator.shm_only="0"
eaccelerator.compress="1"
eaccelerator.compress_level="9"
(6)把常用的程序链接进PATH中
ln -s /usr/local/php/bin/php /usr/bin/php
ln -s /usr/local/php/sbin/php-fpm /usr/bin/phpfpm
ln -s /usr/local/php/bin/pear /usr/bin/pear
ln -s /usr/local/php/bin/pecl /usr/bin/pecl
(7)创建www用户和用户组
/usr/sbin/groupadd www
/usr/sbin/useradd -s /sbin/nologin -g www www
(8)创建php-fpm配置文件
cp -f /usr/local/src/lnmp/php-fpm.conf /usr/local/php/etc/php-fpm.conf
(9)启动php-cgi进程
phpfpm start
(10)设置系统启动自动运行php-cgi
echo "/usr/local/php/sbin/php-fpm start" >> /etc/rc.local

五,编译安装Nginx

(1)安装pcre库,Nginx的伪静态模块需要用到。
cd /usr/local/src/lnmp/pcre-7.9/
./configure
make && make install
(2)安装Nginx
cd /usr/local/src/lnmp/nginx-0.8.52/
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
make && make install
(3)复制配置文件
mkdir -p /usr/local/nginx/conf/vhost/
cp -f /usr/local/src/lnmp/nginx.conf /usr/local/nginx/conf/nginx.conf
cp -f /usr/local/src/lnmp/nginx-0.8.52/conf/fastcgi.conf /usr/local/nginx/conf/fcgi.conf
(3)建立默认站点
cp /usr/local/src/lnmp/default.conf /usr/local/nginx/conf/vhost/default.conf
mkdir /home/www/default
chown -R www:www /home/www/default
(4)安装phpMyAdmin
cp /usr/local/src/lnmp/phpMyAdmin.conf /usr/local/nginx/conf/vhost/phpMyAdmin.conf
cp -r /usr/local/src/lnmp/phpMyAdmin /home/www/
chown -R www:www /home/www/phpMyAdmin

安装之后可以用下面的地址来访问phpMyAdmin

http://您的IP地址:999
(5)复制启动脚本
cp /usr/local/src/lnmp/nginx.init /etc/init.d/nginx
(6)启动Nginx
service nginx start
(7)设置系统启动自动运行Nginx
chkconfig nginx on

六,编译安装PureFTP

(1)安装PureFTP
cd /usr/local/src/lnmp/pure-ftpd-1.0.29
./configure --prefix=/usr/local/pureftpd --with-language=simplified-chinese
make install-strip
(2)设置以Xinetd方式启动

编辑/etc/xinetd.d/pureftpd,加入下面的内容。

service ftp
{
    socket_type = stream
    server = /usr/local/pureftpd/sbin/pure-ftpd
    server_args     = -A -E -I 2 -l unix -p 40000:50000 -w
    protocol = tcp
    user = root
    wait = no
    disable = no
}

重启Xinetd

service xinetd restart
(3)修改www用户的密码
passwd www

输入要设置的密码,然后重复输入一次进行确认。注意Linux下输入的密码不会回显星号。
设置之后即可使用用户名www登录FTP了,密码即为刚刚设置的密码。

七,安装监控程序monitorix

(1)下载配置文件
cd /usr/local/src/lnmp
wget http://wiki.diahosting.com/down/lnmp/cgiwrap-fcgi.pl
wget http://wiki.diahosting.com/down/lnmp/perl_fcgi.conf
wget http://wiki.diahosting.com/down/lnmp/monitorix.conf

cp -f /usr/local/src/lnmp/cgiwrap-fcgi.pl /usr/local/nginx/sbin/cgiwrap-fcgi.pl
chmod 755 /usr/local/nginx/sbin/cgiwrap-fcgi.pl
cp -f /usr/local/src/lnmp/perl_fcgi.conf /usr/local/nginx/conf/perl_fcgi.conf
cp -f /usr/local/src/lnmp/monitorix.conf /usr/local/nginx/conf/vhost/monitorix.conf
(2)安装perl rrdtool crond
rpm --import http://apt.sw.be/RPM-GPG-KEY.dag.txt
cd /usr/local/src/lnmp
wget http://packages.sw.be/rpmforge-release/rpmforge-release-0.5.1-1.el5.rf.i386.rpm
rpm -i rpmforge-release-0.5.1-1.el5.rf.i386.rpm

yum install -y rrdtool rrdtool-perl perl perl-libwww-perl vixie-cron

cd /usr/local/src/lnmp
wget http://www.cpan.org/modules/by-module/FCGI/FCGI-0.67.tar.gz
tar zxvf FCGI-0.67.tar.gz
cd FCGI-0.67
perl Makefile.PL
make
make install

cd /usr/local/src/lnmp
wget http://www.cpan.org/modules/by-module/FCGI/FCGI-ProcManager-0.18.tar.gz
tar zxvf FCGI-ProcManager-0.18.tar.gz
cd FCGI-ProcManager-0.18
perl Makefile.PL
make
make install
(3)启动perl socket
/usr/local/nginx/sbin/cgiwrap-fcgi.pl
echo "/usr/local/nginx/sbin/cgiwrap-fcgi.pl" >> /etc/rc.local
(4)安装monitorix
cd /usr/local/src/lnmp
wget http://www.monitorix.org/monitorix-1.5.2-1.noarch.rpm
rpm -ivh monitorix-1.5.2-1.noarch.rpm
service monitorix start
(5)修改monitorir以适应LNMP环境
sed -i 's/cgi-bin/monitorix/g' /usr/sbin/monitorix.pl
sed -i 's/ENABLE_NGINX = "N/ENABLE_NGINX = "Y/g' /etc/monitorix.conf
sed -i 's/ENABLE_INT = "Y/ENABLE_INT = "N/g' /etc/monitorix.conf
sed -i 's/NGINX_PORT = "80/NGINX_PORT = "998/g' /etc/monitorix.conf
sed -i 's/cgi-bin/monitorix/g' /var/www/html/monitorix/index.html
cp /var/www/cgi-bin/monitorix.cgi /var/www/html/monitorix/monitorix.cgi
echo '<? header("location:/monitorix/index.html") ?>' > /var/www/html/index.php
(6)重启服务
service crond start
service nginx restart
service monitorix restart

* 基本使用

(1)Nginx操作

启动Nginx

service nginx start

重启Nginx

service nginx restart

重新加载Nginx配置文件(不重启)

service nginx reload

停止Nginx

service nginx stop
(2)PHP操作

启动PHP

phpfpm start

重启PHP

phpfpm restart

重新加载PHP配置文件(不重启)

phpfpm reload

停止PHP

phpfpm stop
(3)MySQL操作

启动MySQL

service mysqld start

重启MySQL

service mysqld restart

停止Nginx

service mysqld stop