|
这篇内容不是我原创的,只是将wangyu1314大大的《 Nginx 0.5.33 + PHP 5.2.5(FastCGI)搭建胜过Apache 10倍的Web服务器(第2版)》在ubuntu上进行的一次实践而已。wangyu1314的blog地址是:http://www.cnprint.org/bbs/blogs ... 快去淘宝吧!
我是在vmware下进行测试通过的。同时我在8.04的桌面版上也测试通过了。所有构建过程只是用来学习的,并没有在关键服务中进行过测试,所以请大家不要把这些设置直接用在你的服务上。
ubuntu8.04 server的下载地址可以去www.ubuntu.org.cn去找。安装 ... 任何的服务。
一、设置root权限。
设置root权限,这样下面安装的过程中,不用总sudo了。
[PHP]sudo passwd root[/PHP]
设置好root的密码以后。
[PHP]su -[/PHP]
输入密码,换成root权限。
二、设置apt源。
ubuntu安装完毕以后,默认的apt源速度可能会很慢。所以,首先是修改一下apt的源,提高软件的下载速度。建议大家可以选择ubuntu官方的源,中国台湾省的源和cn99的源。
[PHP]
cp /etc/apt/sources.list /etc/apt/sources.list.bak
nano /etc/apt/sources.list
[/PHP]
删除里面所有的内容,修改成
(白天速度快,晚上速度比较慢。电信和网通都可以)
[PHP]
deb http://archive.ubuntu.com/ubuntu/ hardy main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu/ hardy-security main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu/ hardy-updates main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu/ hardy-proposed main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu/ hardy-backports main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu/ hardy main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu/ hardy-security main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu/ hardy-updates main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu/ hardy-proposed main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu/ hardy-backports main restricted universe multiverse
[/PHP]
(下面的源,推荐电信用户使用。有的时候网通的用户访问速度也非常的快)
[PHP]
deb http://ubuntu.cn99.com/ubuntu/ hardy main restricted universe multiverse
deb http://ubuntu.cn99.com/ubuntu/ hardy-security main restricted universe multiverse
deb http://ubuntu.cn99.com/ubuntu/ hardy-updates main restricted universe multiverse
deb http://ubuntu.cn99.com/ubuntu/ hardy-proposed main restricted universe multiverse
deb http://ubuntu.cn99.com/ubuntu/ hardy-backports main restricted universe multiverse
deb-src http://ubuntu.cn99.com/ubuntu/ hardy main restricted universe multiverse
deb-src http://ubuntu.cn99.com/ubuntu/ hardy-security main restricted universe multiverse
deb-src http://ubuntu.cn99.com/ubuntu/ hardy-updates main restricted universe multiverse
deb-src http://ubuntu.cn99.com/ubuntu/ hardy-proposed main restricted universe multiverse
deb-src http://ubuntu.cn99.com/ubuntu/ hardy-backports main restricted universe multiverse
[/PHP]
(中国台湾省的源,晚上的速度非常的好)
[PHP]
deb http://tw.archive.ubuntu.com/ubuntu hardy main restricted universe multiverse
deb http://tw.archive.ubuntu.com/ubuntu hardy-security main restricted universe multiverse
deb http://tw.archive.ubuntu.com/ubuntu hardy-updates main restricted universe multiverse
deb http://tw.archive.ubuntu.com/ubuntu hardy-backports main restricted universe multiverse
deb http://tw.archive.ubuntu.com/ubuntu hardy-proposed main restricted universe multiverse
deb-src http://tw.archive.ubuntu.com/ubuntu hardy main restricted universe multiverse
deb-src http://tw.archive.ubuntu.com/ubuntu hardy-security main restricted universe multiverse
deb-src http://tw.archive.ubuntu.com/ubuntu hardy-updates main restricted universe multiverse
deb-src http://tw.archive.ubuntu.com/ubuntu hardy-backports main restricted universe multiverse
deb-src http://tw.archive.ubuntu.com/ubuntu hardy-proposed main restricted universe multiverse
[/PHP]
上面的三个源,大家可以根据自己的需要来设置。
保存,退出nano。
[PHP]
apt-get update
[/PHP]
三、安装一些必要的软件。
ubuntu安装以后,有一些包是没有安装上的。我们需要安装上,否则下面的编译将会出现问题。
[PHP]
apt-get install binutils cpp fetchmail flex gcc libarchive-zip-perl libc6-dev libcompress-zlib-perl libdb4.3-dev libpcre3 libpopt-dev lynx m4 make ncftp nmap perl perl-modules unzip zip zlib1g-dev autoconf automake1.9 libtool bison autotools-dev g++ build-essential libpcre3-dev libncurses5-dev libcurl4-openssl-dev openssh-server openssh-client
[/PHP]
四、设置安装目录,下载必要的软件。
安装完毕以后,我们建立几个目录,用来进行下一步的安装。
[PHP]
mkdir /var/software (用来存放安装软件)
mkdir /usr/local/webserver (安装目录,nginx,php等都安装在这个目录下)
mkdir /usr/local/webserver/webdata (发布目录)
mkdir /usr/local/webserver/log (存放nginx的日志文件)
[/PHP]
下载编译需要的各种软件,全部放到/var/software下
mysql-5.0.51a
http://dev.mysql.com/get/Downloa ... uwaterloo.ca/mysql/
php-5.2.6
http://cn.php.net/get/php-5.2.6.tar.gz/from/a/mirror
nginx-0.5.35
http://sysoev.ru/nginx/nginx-0.5.35.tar.gz
libiconv-1.11
http://download.chyangwa.com/linux/libiconv/libiconv-1.11.tar.gz
freetype-2.3.5
http://download.savannah.gnu.org ... eetype-2.3.5.tar.gz
libpng-1.2.29(注意,大家不要使用1.2.28这个版本,似乎有很多问题)
http://jaist.dl.sourceforge.net/ ... ibpng-1.2.29.tar.gz
jpegsrc.v6b
http://down1.chinaunix.net/distfiles/jpegsrc.v6b.tar.gz
libxml2-2.6.30
http://ftp.gnome.org/pub/GNOME/s ... bxml2-2.6.30.tar.gz
libmcrypt-2.5.8
http://nchc.dl.sourceforge.net/s ... mcrypt-2.5.8.tar.gz
mhash-0.9.9
http://jaist.dl.sourceforge.net/ ... /mhash-0.9.9.tar.gz
mcrypt-2.6.7
http://jaist.dl.sourceforge.net/ ... mcrypt-2.6.7.tar.gz
memcache-2.2.3
http://pecl.php.net/get/memcache-2.2.3.tgz
pcre-7.6
http://nchc.dl.sourceforge.net/sourceforge/pcre/pcre-7.6.tar.gz
zend
http://www.phpchina.com/download ... ;sort=ZendOptimizer
五、编译安装PHP所需的支持库。
[PHP]
cd /var/software
tar zxvf libiconv-1.11.tar.gz
cd libiconv-1.11/
./configure --prefix=/usr/local
make
make install
cd ../
tar zxvf freetype-2.3.5.tar.gz
cd freetype-2.3.5/
./configure
make
make install
cd ../
tar zxvf libpng-1.2.29.tar.gz
cd libpng-1.2.29/
./configure
###mv scripts/makefile.linux ./makefile
make
make install
cd ../
tar zxvf jpegsrc.v6b.tar.gz
cd jpeg-6b/
./configure --enable-static --enable-shared
make
make install
make install-lib
cd ../
tar zxvf libxml2-2.6.30.tar.gz
cd libxml2-2.6.30/
./configure
make
make install
cd ../
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 ../../
tar zxvf mhash-0.9.9.tar.gz
cd mhash-0.9.9/
./configure
make
make install
cd ../
cp /usr/local/lib/libmcrypt.* /usr/lib
ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2
tar zxvf mcrypt-2.6.7.tar.gz
cd mcrypt-2.6.7/
./configure
make
make install
cd ../
[/PHP]
六、编译安装MySQL
[PHP]
tar zxvf mysql-5.0.51b.tar.gz
cd mysql-5.0.51b
./configure --prefix=/usr/local/webserver/mysql --localstatedir=/var/lib/mysql --with-comment=Source --with-server-suffix=-enterprise-gpl --with-mysqld-user=mysql --without-debug --with-unix-socket-path=/tmp/mysql.sock --with-big-tables --with-extra-charsets=gbk,latin1,utf8 --with-pthread --enable-static --with-client-ldflags=-all-static --with-mysqld-ldflags=-all-static --enable-assembler --without-innodb --without-ndb-debug --without-isam --enable-local-infile --with-readline --with-raid --enable-thread-safe-client
make
make install
groupadd mysql
useradd -g mysql mysql
cd /usr/local/webserver/mysql
bin/mysql_install_db --user=mysql
chown -R root:mysql . (设置权限,注意后面有一个 ".")
chown -R mysql /var/lib/mysql (设置 mysql 目录权限。数据库就放在这里了)
chgrp -R mysql . (注意后面有一个 ".")
cp share/mysql/my-medium.cnf /etc/my.cnf
cp share/mysql/mysql.server /etc/init.d/mysqld (为了开机可以自动启动 mysql。)
chmod 755 /etc/init.d/mysqld
echo "/usr/local/webserver/mysql/lib" >> /etc/ld.so.conf (添加LIB PATH)
运行以下命令即可启动 MySQL 服务器:
/etc/init.d/mysqld start (启动 MySQL)
bin/mysqladmin -u root password "password_for_root"(设置mysql的root密码)
/etc/init.d/mysqld stop (关闭 MySQL)
nano /etc/rc.local
在最后加入:/etc/init.d/mysqld start(这样开机的时候,mysql就可以自动启动了。)
[/PHP]
七、编译安装PHP(FastCGI模式)
[PHP]
cd /var/software
tar zxvf php-5.2.6.tar.gz
cd php-5.2.6
./configure --prefix=/usr/local/webserver/php --with-config-file-path=/usr/local/webserver/php/etc --with-mysql=/usr/local/webserver/mysql --with-iconv-dir=/usr/local --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir --enable-xml --disable-debug --enable-discard-path --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fastcgi --enable-force-cgi-redirect --enable-mbstring --with-mcrypt --disable-ipv6 --enable-soap --with-openssl --with-gettext --disable-rpath
sed -i 's#-lz -lm -lxml2 -lz -lm -lxml2 -lz -lm -lcrypt#& -liconv#' Makefile
make
make install
cp php.ini-dist /usr/local/webserver/php/etc/php.ini
cd ../
[/PHP]
八、编译安装PHP5扩展模块
[PHP]
tar zxvf memcache-2.2.3.tgz
cd memcache-2.2.3/
/usr/local/webserver/php/bin/phpize
./configure --with-php-config=/usr/local/webserver/php/bin/php-config
make
make install
cd ../
cd php-5.2.6/ext/gd/
/usr/local/webserver/php/bin/phpize
./configure --with-jpeg-dir --with-png-dir --with-zlib-dir --with-ttf --with-freetype-dir --with-php-config=/usr/local/webserver/php/bin/php-config
make
make install
cd ../../../
[/PHP]
九、对php进行一些设置,并且为安装nginx做一些准备工作。
1、修改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/"
并在此行后增加以下几行,然后保存。
[PHP]
extension = "memcache.so"
extension = "gd.so"
[/PHP]
自动修改:若嫌手工修改麻烦,可执行以下shell命令,自动完成对php.ini文件的修改:
[PHP]
sed -i 's#extension_dir = "./"#extension_dir = "/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/"\nextension = "memcache.so"\nextension = "gd.so"\n#' /usr/local/webserver/php/etc/php.ini
[/PHP]
2、创建www用户和组,以及其使用的目录。
[PHP]
/usr/sbin/groupadd www -g 48
/usr/sbin/useradd -u 48 -g www www
chmod +w /usr/local/webserver/webdata/
chown -R www:www /usr/local/webserver/webdata/
[/PHP]
3、安装lighttpd中附带的spawn-fcgi,用来启动php-cgi
[PHP]
cp spawn-fcgi /usr/local/webserver/php/bin
chmod +x /usr/local/webserver/php/bin/spawn-fcgi
[/PHP]
注:spawn-fcgi我在网站没有找到,大家可以去wangyu1314大大的blog里面找他打得一个包文件。地址是:http://ishare.iask.sina.com.cn/cgi-bin/fileid.cgi?fileid=2746375。压缩包中的spawn-fcgi程序为已经编译成二进制的版本。顺便说一下,大家可以去http://www.cnprint.org/bbs/blogs/1/blog101.html 下载wangyu1314大大做好的全套的安装软件包。精品啊!
4、启动php-cgi进程,监听127.0.0.1的10080端口,进程数为64(可以根据自己的情况来设置。如果服务器内存小于3GB,可以只开启25个进程。),用户为www。
[PHP]
/usr/local/webserver/php/bin/spawn-fcgi -a 127.0.0.1 -p 10080 -C 64 -u www -f /usr/local/webserver/php/bin/php-cgi
[/PHP]
十、安装Nginx 0.5.33
1、安装Nginx所需的pcre库。
[PHP]
tar zxvf pcre-7.6.tar.gz
cd pcre-7.6/
./configure
make && make install
cd ../
[/PHP]
2、安装Nginx。
[PHP]
tar zxvf nginx-0.5.35.tar.gz
cd nginx-0.5.35/
./configure --user=www --group=www --prefix=/usr/local/webserver/nginx --with-http_stub_status_module
make
make install
cd ../
[/PHP]
3、创建Nginx日志目录。
[PHP]
chmod +w /usr/local/webserver/logs
chown -R www:www /usr/local/webserver/logs
[/PHP]
4、创建Nginx配置文件。
在/usr/local/webserver/nginx/conf/目录中创建nginx.conf文件。
[PHP]
rm -f /usr/local/webserver/nginx/conf/nginx.conf
nano /usr/local/webserver/nginx/conf/nginx.conf
[/PHP]
输入以下内容。
[PHP]
user www www;
worker_processes 10;
error_log /usr/local/webserver/logs/nginx_error.log crit;
#pid logs/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 51200;
events
{
use epoll;
worker_connections 51200;
}
http
{
include conf/mime.types;
default_type application/octet-stream;
charset gb2312;
server_names_hash_bucket_size 128;
#sendfile on;
#tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 8k;
gzip_http_version 1.1;
gzip_types text/plain application/x-javascript text/css text/html application/xml;
server
{
listen 80;
server_name servername.com;
index index.html index.htm index.php;
root /usr/local/webserver/webdata;
if (-d $request_filename)
{
rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
}
location ~ .*\.php?$
{
include conf/fcgi.conf;
fastcgi_pass 127.0.0.1:10080;
fastcgi_index index.php;
}
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
access_log /usr/local/webserver/logs/access.log access;
}
server
{
listen 80;
server_name status.servername.com;
location / {
stub_status on;
access_log off;
}
}
}
[/PHP]
保存,退出nano。
5、在/usr/local/webserver/nginx/conf/目录中创建fcgi.conf文件。
[PHP]
nano /usr/local/webserver/nginx/conf/fcgi.conf
[/PHP]
输入以下内容:
[PHP]
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
# PHP only, required if PHP was built with --enable-force-cgi-redirect
#fastcgi_param REDIRECT_STATUS 200;
[/PHP]
6、启动Nginx
[PHP]
ulimit -SHn 51200
/usr/local/webserver/nginx/sbin/nginx -c /usr/local/webserver/nginx/conf/nginx.conf
[/PHP]
7、配置开机自动启动Nginx + PHP
[PHP]
nano /etc/rc.local
[/PHP]
在末尾增加以下内容:
[PHP]
ulimit -SHn 51200
/usr/local/webserver/php/bin/spawn-fcgi -a 127.0.0.1 -p 10080 -C 64 -u www -f /usr/local/webserver/php/bin/php-cgi
/usr/local/webserver/nginx/sbin/nginx -c /usr/local/webserver/nginx/conf/nginx.conf
[/PHP]
十一、优化Linux内核参数
[PHP]
nano /etc/sysctl.conf
[/PHP]
在末尾增加以下内容.
[PHP]
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 300
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.ip_local_port_range = 5000 65000
[/PHP]
使配置立即生效:
[PHP]
/sbin/sysctl -p
[/PHP]
十二、在不停止Nginx服务的情况下平滑变更Nginx配置
修改/usr/local/webserver/nginx/conf/nginx.conf配置文件后,请执行以下命令检查配置文件是否正确.
[PHP]
/usr/local/webserver/nginx/sbin/nginx -t
[/PHP]
如果屏幕显示以下两行信息,说明配置文件正确。
[PHP]
the configuration file /usr/local/webserver/nginx/conf/nginx.conf syntax is ok
the configuration file /usr/local/webserver/nginx/conf/nginx.conf was tested successfully
[/PHP]
这时,输入以下命令查看Nginx主进程号。
[PHP]
ps -ef | grep "nginx: master process" | grep -v "grep" | awk -F ' ' '{print $2}'
[/PHP]
屏幕显示的即为Nginx主进程号,例如:
4343
这时,执行以下命令即可使修改过的Nginx配置文件生效.
[PHP]
kill -HUP 4343
[/PHP]
十三、安装zend
[PHP]
cd /var/software
tar zxvf ZendOptimizer-3.3.3-linux-glibc23-i386.tar.gz
cd ZendOptimizer-3.3.3-linux-glibc23-i386
./install
[/PHP]
在询问php.ini的路径的时候,填入.
[PHP]
/usr/local/webserver/php/etc
[/PHP]
web服务器选择不是apache
十四、reboot你的机器。 |
|