VPS 安装Nginx+MySQL+PHP 5.2.17(FastCGI)服务器环境(Debian6)备忘

CentOS 5.3,和RedHat请参考张宴“Nginx0.8.x + PHP 5.2.13(FastCGI)搭建胜过Apache十倍的Web服务器(第6版):http://blog.s135.com/nginx_php_v6/

附件:debian6服务器环境 Nginx MySQL php5.2.17(FastCGI)服务器环境配置备忘

debian6服务器环境:
cat /proc/version
Linux version 2.6.18-194.26.1.el5.028stab079.2 (root@rhel5-build-x64) (gcc version 4.1.2 20080704 (Red Hat 4.1.2-46)) #1 SMP Fri Dec 17 19:25:15 MSK 2010
root@vps:~# cat /etc/issue
Debian GNU/Linux 6.0 \n \l

root@vps:~# uname -a
Linux vps 2.6.18-194.26.1.el5.028stab079.2 #1 SMP Fri Dec 17 19:25:15 MSK 2010 i686 GNU/Linux

 

photonvps买了个VPS,装上了debian6,然后装shopex的,折腾那个zend,结果debian默认是php3的,本来内存不大,干脆就直接卸掉Apache,换用Nginx了,还是编译安装php5.2装shopex吧。(话说shopex对这个“高”版本的mysql 5.5.3也是不能很好支持的,最后是直接用原来空间里的备份文件恢复进去的,直接新装是悲剧的,安装系统数据失败什么的,总之shopex真挺拽的!!)
用netstat -ntlp查检一下系统中启动的哪些服务

删除sendmail named(bind9)
/etc/init.d/sendmail stop
/etc/init.d/apache2 stop
/etc/init.d/bind9 stop
/etc/init.d/samba stop
dpkg -l |grep sendmail
apt-get remove sendmail sendmail-base sendmail-bin sendmail-cf sendmail-doc
dpkg -l |grep sendmail
dpkg –force-all -P sendmail-base sendmail-bin sendmail-cf sendmail-doc

dpkg -l |grep apache
apt-get remove apache2 apache2-doc apache2-mpm-prefork apache2-utils apache2.2-bin apache2.2-common
dpkg -l |grep apache
dpkg –force-all -P apache2-doc apache2.2-common

dpkg -l |grep bind9
apt-get remove bind9 bind9-host bind9utils libbind9-60
dpkg -l |grep bind9
dpkg –force-all -P bind9 libbind9-60

dpkg -l |grep samba
apt-get remove samba samba-common
dpkg -l |grep samba
dpkg –force-all -P samba samba-common
安装源码编译的环境
apt-get install build-essential

 

 

获取相关开源程序
mkdir /root/soft
cd /root/soft
vim down.sh
内容如下:
wget http://sysoev.ru/nginx/nginx-0.8.34.tar.gz
wget http://blog.s135.com/soft/linux/nginx_php/php/php-5.2.14.tar.gz
wget http://php-fpm.org/downloads/php-5.2.14-fpm-0.5.14.diff.gz
wget http://blog.s135.com/soft/linux/nginx_php/mysql/mysql-5.5.3-m3.tar.gz
wget http://blog.s135.com/soft/linux/nginx_php/libiconv/libiconv-1.13.1.tar.gz
wget http://blog.s135.com/soft/linux/nginx_php/mcrypt/libmcrypt-2.5.8.tar.gz
wget http://blog.s135.com/soft/linux/nginx_php/mcrypt/mcrypt-2.6.8.tar.gz
wget http://blog.s135.com/soft/linux/nginx_php/memcache/memcache-2.2.5.tgz
wget “http://downloads.sourceforge.net/mhash/mhash-0.9.9.9.tar.gz?modtime=1175740843&big_mirror=0”
wget http://blog.s135.com/soft/linux/nginx_php/pcre/pcre-8.10.tar.gz
wget http://blog.s135.com/soft/linux/nginx_php/eaccelerator/eaccelerator-0.9.6.1.tar.bz2
wget http://pecl.php.net/get/PDO_MYSQL-1.0.2.tgz
wget http://blog.s135.com/soft/linux/nginx_php/imagick/ImageMagick.tar.gz
wget http://pecl.php.net/get/imagick-2.3.0.tgz
设置可执行权限
chmod +x down.sh
开始下载
./down.sh
安装PHP 5.2.13(FastCGI模式)
编译安装PHP 5.2.13所需的支持库
tar zxvf libiconv-1.13.1.tar.gz
cd libiconv-1.13.1/
./configure –prefix=/usr/local
make && make install
cd ../
tar zxvf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8/
/sbin/ldconfig
cd libltdl/
./configure –-enable-ltdl-install
make && make install
cd ..
./configure
make && make install
cd ../

tar zxvf mhash-0.9.9.9.tar.gz
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
tar zxvf mcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8/
/sbin/ldconfig
./configure
make && make install
cd ../
编译安装MySQL-5.5.3-m3
mysql安装前需要安装 libncurses5-dev
apt-get install libncurses5-dev

可以开始编译mysql

groupadd mysql
useradd -g mysql mysql
tar zxvf mysql-5.5.3-m3.tar.gz
cd mysql-5.5.3-m3/
./configure –-prefix=/usr/local/webserver/mysql/ –-enable-assembler –-with-extra-charsets=complex –-enable-thread-safe-client –-with-big-tables -–with-readline –-with-ssl –-with-embedded-server –-enable-local-infile -–with-plugins=partition,innobase,myisammrg
make && make install
chmod +w /usr/local/webserver/mysql
chown -R mysql:mysql /usr/local/webserver/mysql
cd ../
①、创建MySQL数据库存放目录
mkdir -p /data0/mysql/3306/data/
mkdir -p /data0/mysql/3306/binlog/
mkdir -p /data0/mysql/3306/relaylog/
chown -R mysql:mysql /data0/mysql/
②、以mysql用户帐号的身份建立数据表
/usr/local/webserver/mysql/bin/mysql_install_db –-basedir=/usr/local/webserver/mysql –-datadir=/data0/mysql/3306/data –-user=mysql

③、创建my.cnf配置文件
vi /data0/mysql/3306/my.cnf
内容参考附件
④、创建管理MySQL数据库的shell脚本
vi /data0/mysql/3306/mysql
输入内容参考附件(这里的用户名admin和密码12345678接下来的步骤会创建)

⑤、赋予shell脚本可执行权限
chmod +x /data0/mysql/3306/mysql ⑥、启动MySQL
/data0/mysql/3306/mysql start ⑦、设置MySql root用户的密码(记住,后面用到)
/usr/local/webserver/mysql/bin/mysqladmin -u root password ’123456789′ ⑧、通过命令行登录管理MySQL服务器(提示输入密码时,输入刚才设置的密码)
/usr/local/webserver/mysql/bin/mysql -u root -p -S /tmp/mysql.sock
这时候到mysql命令行中
⑨、输入以下SQL语句,创建一个具有root权限的用户(admin)和密码(12345678)
GRANT ALL PRIVILEGES ON *.* TO ‘admin’@’localhost’ IDENTIFIED BY ’12345678′;
GRANT ALL PRIVILEGES ON *.* TO ‘admin’@’127.0.0.1′ IDENTIFIED BY ’12345678′;
exit
⑩、(可选)停止MySQL
/data0/mysql/3306/mysql stop

 

编译安装PHP(FastCGI模式)扩展
我们先来看一下下面这两个命令的作用
apt-cache search “freetype”
这个命令是在源中查找和freetype有关的所有软件,然后再根据软件包的描述,找你自己需要的软件,然后就可以用下面的命令进行安装
apt-get install libfreetype6-dev
当然是要根据错误提示来查找相关的软件。
我们在编译过程中,缺什么,就补什么,这个也算是一种解决问题的思路。希望能给大家带来点帮助!
下面也是同样的道理,我就简写了。
tar zxvf php-5.2.14.tar.gz
gzip -cd php-5.2.14-fpm-0.5.14.diff.gz | patch -d php-5.2.14 -p1
cd php-5.2.13/
./configure –prefix=/usr/local/webserver/php –with-config-file-path=/usr/local/webserver/php/etc –with-mysql=/usr/local/webserver/mysql –with-mysqli=/usr/local/webserver/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
make ZEND_EXTRA_LIBS=’-liconv’
make install
cp php.ini-dist /usr/local/webserver/php/etc/php.ini
cd ../
如果出现下面的错误提示
checking for xml2-config… no
checking for xml-config… no
configure: error: XML configuration could not be found
安装
apt-get install libxml2-dev
出现
configure: error: Cannot find OpenSSL’s <evp.h>
安装
apt-get install libssl-dev
出现
configure: error: Please reinstall the libcurl distribution –
easy.h should be in <curl-dir>/include/curl/
安装
apt-get install libcurl4-openssl-dev
出现
configure: error: libjpeg.(a|so) not found.
安装
apt-get install libjpeg62-dev
出现
configure: error: libpng.(a|so) not found.
安装
apt-get install libpng12-dev
出现
configure: error: freetype.h not found.
安装
apt-get install libfreetype6-dev
出现
configure: error: sasl.h not found!
安装
apt-get install libsasl2-dev

编译安装PHP5扩展模块
在这一步中,我们需要安装autoconf这个软件包
apt-get install autoconf
tar zxvf memcache-2.2.5.tgz
cd memcache-2.2.5/
/usr/local/webserver/php/bin/phpize
./configure –with-php-config=/usr/local/webserver/php/bin/php-config
make && make install
cd ../
tar jxvf eaccelerator-0.9.6.1.tar.bz2
cd eaccelerator-0.9.6.1/
/usr/local/webserver/php/bin/phpize
./configure –enable-eaccelerator=shared –with-php-config=/usr/local/webserver/php/bin/php-config
make && make install
cd ../
tar zxvf PDO_MYSQL-1.0.2.tgz
cd PDO_MYSQL-1.0.2/
/usr/local/webserver/php/bin/phpize
./configure –with-php-config=/usr/local/webserver/php/bin/php-config –with-pdo-mysql=/usr/local/webserver/mysql
make && make install
cd ../
安装 ImageMagick的时候make会出现
/usr/bin/ld: cannot find -lperl
这时候就要安装
apt-get install libperl-dev
tar zxvf ImageMagick.tar.gz
cd ImageMagick-6.5.1-2/
./configure
make && make install
cd ../
tar zxvf imagick-2.3.0.tgz
cd imagick-2.3.0/
/usr/local/webserver/php/bin/phpize
./configure –with-php-config=/usr/local/webserver/php/bin/php-config
make && make install
cd ../
5、修改php.ini文件
手工修改:
查找/usr/local/webserver/php/etc/php.ini中的
extension_dir = “./”
修改为
extension_dir = “/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/”
并在此行后增加以下几行,然后保存:
extension = “memcache.so”
extension = “pdo_mysql.so”
extension = “imagick.so”
再查找
output_buffering = Off
修改为
output_buffering = On
自动修改:
sed -i ‘s#extension_dir = “./”#extension_dir = “/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/”\nextension = “memcache.so”\nextension = “pdo_mysql.so”\nextension = “imagick.so”\n#’ /usr/local/webserver/php/etc/php.ini
sed -i ‘s#output_buffering = Off#output_buffering = On#’ /usr/local/webserver/php/etc/php.ini
sed -i “s#; always_populate_raw_post_data = On#always_populate_raw_post_data = On#g” /usr/local/webserver/php/etc/php.ini
6、配置eAccelerator加速PHP
mkdir -p /usr/local/webserver/eaccelerator_cache
vi /usr/local/webserver/php/etc/php.ini
到配置文件的最末尾,加上以下配置信息
[eaccelerator]
zend_extension=”/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so”
eaccelerator.shm_size=”64″
eaccelerator.cache_dir=”/usr/local/webserver/eaccelerator_cache”
eaccelerator.enable=”1″
eaccelerator.optimizer=”1″
eaccelerator.check_mtime=”1″
eaccelerator.debug=”0″
eaccelerator.filter=””
eaccelerator.shm_max=”0″
eaccelerator.shm_ttl=”3600″
eaccelerator.shm_prune_period=”3600″
eaccelerator.shm_only=”0″
eaccelerator.compress=”1″
eaccelerator.compress_level=”9″
7、创建www用户和组,以及供debian.freeopens.com虚拟主机使用的目录
/usr/sbin/groupadd www
/usr/sbin/useradd -g www www
mkdir -p /data0/htdocs/www
chmod +w /data0/htdocs/www
chown -R www:www /data0/htdocs/www
8、创建php-fpm配置文件(php-fpm是为PHP打的一个FastCGI管理补丁,可以平滑变更php.ini配置而无需重启php-cgi)
在/usr/local/webserver/php/etc/目录中创建php-fpm.conf文件
rm -f /usr/local/webserver/php/etc/php-fpm.conf
vi /usr/local/webserver/php/etc/php-fpm.conf
内容参考附件

 

9、启动php-cgi进程,监听127.0.0.1的9000端口,进程数为128(如果服务器内存小于3GB,可以只开启64个进程),用户为www:
ulimit -SHn 65535
/usr/local/webserver/php/sbin/php-fpm start
注:/usr/local/webserver/php/sbin/php-fpm还有其他参数,包括:start|stop|quit|restart|reload|logrotate,修改php.ini后不重启php-cgi,重新加载配置文件使用reload。
如果出现下面的错误(这个错误是我在安装过程中发现的,是内在不足导致的,在文章的结尾优化后,就不会有这样的问题了,在这里做个记录,方便大家学习)
Starting php_fpm eAccelerator: Could not allocate 67108864 bytes, the maximum size the kernel allows is 33554432 bytes. Lower the amount of memory request or increase the limit in /proc/sys/kernel/shmmax.
PHP Warning: [eAccelerator] Can not create shared memory area in Unknown on line 0
PHP Fatal error: Unable to start eAccelerator module in Unknown on line 0
failed

 

安装Nginx 0.8.34
1、安装Nginx所需的pcre库
tar zxvf pcre-8.10.tar.gz
cd pcre-8.10/
./configure
make && make install
cd ../
2、安装Nginx
tar zxvf nginx-0.8.34.tar.gz
cd nginx-0.8.34/
./configure –user=www –group=www –prefix=/usr/local/webserver/nginx –with-http_stub_status_module –with-http_ssl_module
make && make install
cd ../
3、创建Nginx日志目录
mkdir -p /data1/logs
chmod +w /data1/logs
chown -R www:www /data1/logs
4、创建Nginx配置文件
①、在/usr/local/webserver/nginx/conf/目录中创建nginx.conf文件:
rm -f /usr/local/webserver/nginx/conf/nginx.conf
vi /usr/local/webserver/nginx/conf/nginx.conf
输入内容参考附件

②、在/usr/local/webserver/nginx/conf/目录中创建fcgi.conf文件
vi /usr/local/webserver/nginx/conf/fcgi.conf
5、启动Nginx
ulimit -SHn 65535
/usr/local/webserver/nginx/sbin/nginx
如果出现下面的错误
usr/local/webserver/nginx/sbin/nginx: error while loading shared libraries: libpcre.so.0: cannot open shared object file: No such file or directory
安装
apt-get install libpcre3-dev

 

配置开机自动启动Nginx + PHP + Mysql
vi /etc/rc.local
把下面的内容增加到exit 0的上面
/data0/mysql/3306/mysql start
ulimit -SHn 65535
/usr/local/webserver/php/sbin/php-fpm start
/usr/local/webserver/nginx/sbin/nginx
相关软件的重新启动
如果说你改了nginx的配置文件,就要用下面的命令,使配置生效
/usr/local/webserver/nginx/sbin/nginx -t
对于Nginx 0.8.x版本,现在平滑重启Nginx配置非常简单,执行以下命令即可
/usr/local/webserver/nginx/sbin/nginx -s reload
2)对于Nginx 0.8.x之前的版本,平滑重启稍微麻烦一些,按照以下步骤进行即可。输入以下命令查看Nginx主进程号
ps -ef | grep “nginx: master process” | grep -v “grep” | awk -F ‘ ‘ ‘{print $2}’
屏幕显示的即为Nginx主进程号,例如:6302
这时,执行以下命令即可使修改过的Nginx配置文件生效:kill -HUP 6302

每天定时切割Nginx日志
1、创建脚本/usr/local/webserver/nginx/sbin/cut_nginx_log.sh
vi /usr/local/webserver/nginx/sbin/cut_nginx_log.sh
输入以下内容
#!/bin/bash
# This script run at 00:00

# The Nginx logs path
logs_path=”/usr/local/webserver/nginx/logs/”

mkdir -p ${logs_path}$(date -d “yesterday” +”%Y”)/$(date -d “yesterday” +”%m”)/
mv ${logs_path}access.log ${logs_path}$(date -d “yesterday” +”%Y”)/$(date -d “yesterday” +”%m”)/access_$(date -d “yesterday” +”%Y%m%d”).log
kill -USR1 `cat /usr/local/webserver/nginx/nginx.pid`
2、设置crontab,每天凌晨00:00切割nginx访问日志 crontab -e
增加以下内容
00 00 * * * /bin/bash /usr/local/webserver/nginx/sbin/cut_nginx_log.sh

 

附件地址:http://clin003.com/wp-content/uploads/2012/10/new-www.txt

One Reply to “VPS 安装Nginx+MySQL+PHP 5.2.17(FastCGI)服务器环境(Debian6)备忘”

Comments are closed.