ubuntu下配置nginx+php+mysql+zend
导出新系统ubuntu server 8.04安装history.
1 cd /home/
2 wget http://192.168.1.100/nginx-php.tar.gz
3 apt-get install libxml2
4 apt-get install xml2-config
5 apt-get install libxml-dev
6 cp /etc/apt/sources.list /etc/apt/sources.list.bak
7 nano /etc/apt/sources.list
8 apt-get update
9 apt-get install libxml-dev
10 apt-get install xml2
11 apt-get install zlib1g-dev
12 apt-get install libz-dev
13 apt-get install zlib1g-dev
14 apt-get install libxml2-dev
15 apt-get install curl
16 apt-get install curl-dev
17 apt-get install libcurl
18 apt-get install curl
19 apt-get install libjpeg
20 apt-get install libjpeg-dev
21 apt-get install libpng-dev
22 apt-get install libfreetype6
23 apt-get install libfreetype6-dev
24 apt-get install php5-curl
25 apt-get install mcrypt
26 apt-get install libmcrypt
27 apt-get install mcrypt-dev
28 apt-get install libmcrypt-dev
29 ls
30 cd /usr/sbin/
31 ls
32 /usr/sbin/groupadd mysql
33 /usr/sbin/useradd -g mysql mysql
34 cd /home/
35 tar zxvf nginx-php.tar.gz
36 cd nginx-php
37 ls
38 tar zxvf mysql-5.0.45.tar.gz
39 ./configure –prefix=/usr/local/webserver/mysql/ –without-debug –with-unix-socket-path=/tmp/mysql.sock –with-client-ldflags=-all-static –with-mysqld-ldflags=-all-static –enable-assembler –with-extra-charsets=gbk,gb2312,utf8 –with-pthread –enable-thread-safe-client
40 cd mysql-5.0.45
41 ./configure –prefix=/usr/local/webserver/mysql/ –without-debug –with-unix-socket-path=/tmp/mysql.sock –with-client-ldflags=-all-static –with-mysqld-ldflags=-all-static –enable-assembler –with-extra-charsets=gbk,gb2312,utf8 –with-pthread –enable-thread-safe-client
42 apt-get install make gcc g++ c++
43 apt-get install make
44 apt-get install gcc
45 apt-get install g++
46 apt-get install c++
47 ./configure –prefix=/usr/local/webserver/mysql/ –without-debug –with-unix-socket-path=/tmp/mysql.sock –with-client-ldflags=-all-static –with-mysqld-ldflags=-all-static –enable-assembler –with-extra-charsets=gbk,gb2312,utf8 –with-pthread –enable-thread-safe-client
48 apt-get install libncurses5-dev
49 ./configure –prefix=/usr/local/webserver/mysql/ –without-debug –with-unix-socket-path=/tmp/mysql.sock –with-client-ldflags=-all-static –with-mysqld-ldflags=-all-static –enable-assembler –with-extra-charsets=gbk,gb2312,utf8 –with-pthread –enable-thread-safe-client
50 make
51 make install
52 cd /usr/local/webserver/mysql/
53 ls
54 chmod +w /usr/local/webserver/mysql
55 chown -R mysql:mysql /usr/local/webserver/mysql
56 cd /home/nginx-php/mysql-5.0.45
57 cp support-files/my-medium.cnf /usr/local/webserver/mysql/my.cnf
58 /usr/local/webserver/mysql/bin/mysql_install_db –defaults-file=/usr/local/webserver/mysql/my.cnf –basedir=/usr/local/webserver/mysql –datadir=/usr/local/webserver/mysql/data –user=mysql –pid-file=/usr/local/webserver/mysql/mysql.pid –skip-locking –port=3306 –socket=/tmp/mysql.sock
59 top
60 /usr/local/webserver/mysql/bin/mysql_install_db –defaults-file=/usr/local/webserver/mysql/my.cnf –basedir=/usr/local/webserver/mysql –datadir=/usr/local/webserver/mysql/data –user=mysql –pid-file=/usr/local/webserver/mysql/mysql.pid –skip-locking –port=3306 –socket=/tmp/mysql.sock
61 /bin/sh /usr/local/webserver/mysql/bin/mysqld_safe –defaults-file=/usr/local/webserver/mysql/my.cnf &
62 cd /var/run
63 ls
64 /bin/sh /usr/local/webserver/mysql/bin/mysqld_safe –defaults-file=/usr/local/webserver/mysql/my.cnf &
65 /usr/local/webserver/mysql/bin/mysqld_safe –defaults-file=/usr/local/webserver/mysql/my.cnf &
66 ls
67 clear
68 top
69 mysql
70 apt-get install mysql-client-5.0
71 mysql -u root -p
72 mysql -u root -p -S /tmp/mysql.sock
73 ./configure –prefix=/usr/local/webserver/php –with-config-file-path=/usr/local/webserver/php/etc –with-mysql=/usr/local/webserver/mysql –with-iconv-dir –with-freetype-dir –with-jpeg-dir –with-png-dir –with-zlib –with-libxml-dir –enable-xml –disable-debug –disable-rpath –enable-discard-path –enable-safe-mode –enable-bcmath –enable-shmop –enable-sysvsem –enable-inline-optimization –with-curlwrappers –enable-mbregex –enable-fastcgi –enable-force-cgi-redirect –enable-mbstring –with-mcrypt –with-mysqli=/usr/local/webserver/mysql/bin/mysql_config –with-zlib-dir –with-ttf –with-gd
74 cd /home/nginx-php
75 ls
76 wget http://cn2.php.net/get/php-5.2.6.tar.gz/from/cn.php.net/mirror
77 tar zxvf php-5.2.6.tar.gz
78 cd php-5.2.6
79 ls
80 ./configure –prefix=/usr/local/webserver/php –with-config-file-path=/usr/local/webserver/php/etc –with-mysql=/usr/local/webserver/mysql –with-iconv-dir –with-freetype-dir –with-jpeg-dir –with-png-dir –with-zlib –with-libxml-dir –enable-xml –disable-debug –disable-rpath –enable-discard-path –enable-safe-mode –enable-bcmath –enable-shmop –enable-sysvsem –enable-inline-optimization –with-curlwrappers –enable-mbregex –enable-fastcgi –enable-force-cgi-redirect –enable-mbstring –with-mcrypt –with-mysqli=/usr/local/webserver/mysql/bin/mysql_config –with-zlib-dir –with-ttf –with-gd
81 cd ../
82 ls
83 tar zxvf memcache-2.2.1.tgz
84 cd memcache-2.2.1
85 ls
86 cd ../php-5.2.6
87 make
88 sed -i ’s#-lz -lm -lxml2 -lz -lm -lxml2 -lz -lm -lcrypt#& -liconv#’ Makefile
89 sed -i ’s#-lz -lm -lxml2 -lz -lm -lxml2 -lz -lm -lcrypt#& -liconv#’ Makefile
90 sed -i ’s#-lz -lm -lxml2 -lz -lm -lxml2 -lz -lm -lcrypt#& -liconv#’ Makefile
91 make
92 make test
93 make install
94 cp php.ini-dist /usr/local/webserver/php/etc/php.ini
95 cd ../
96 cd memcache-2.2.1
97 /usr/local/webserver/php/bin/phpize
98 ./configure –with-php-config=/usr/local/webserver/php/bin/php-config
99 ./configure –with-php-config=/usr/local/webserver/php/bin/php-config
100 ls
101 ./configure –with-php-config=/usr/local/webserver/php/bin/php-config
102 /usr/local/webserver/php/bin/phpize
103 nano /usr/local/webserver/php/bin/phpize
104 /usr/local/webserver/php/bin/phpize
105 nano /usr/local/webserver/php/bin/phpize
106 apt-get install autoconf
107 /usr/local/webserver/php/bin/phpize
108 ./configure –with-php-config=/usr/local/webserver/php/bin/php-config
109 /usr/local/webserver/php/bin/phpize
110 apt-get install autoconf
111 ./configure –with-php-config=/usr/local/webserver/php/bin/php-config
112 ls
113 ./config.guess –with-php-config=/usr/local/webserver/php/bin/php-config
114 ./config.sub –with-php-config=/usr/local/webserver/php/bin/php-config
115 cd ../
116 ls
117 cd php-5.2.5/ext/gd/
118 cd php-5.2.6/ext/gd/
119 /usr/local/webserver/php/bin/phpize
120 cd ../../../
121 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
122 /usr/sbin/groupadd www -g 48
123 /usr/sbin/useradd -u 48 -g www www
124 mkdir /home/webroot
125 chmod +w /home/webroot/
126 chown -R www:www /home/webroot/
127 cp spawn-fcgi /usr/local/webserver/php/bin
128 chmod +x /usr/local/webserver/php/bin/spawn-fcgi
129 /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
130 apt-get install pcre
131 apt-get install libpcre
132 apt-get install libpcre-dev
133 apt-get install libpcre3
134 ls
135 tar zxvf nginx-0.5.33.tar.gz
136 cd nginx-0.5.33
137 apt-get install libpcre3-dev
138 ./configure –user=www –group=www –prefix=/usr/local/webserver/nginx –with-http_stub_status_module
139 make && make install
140 nano /usr/local/webserver/nginx/conf/nginx.conf
141 nano /usr/local/webserver/nginx/conf/fcgi.conf
142 ulimit -SHn 51200
143 /usr/local/webserver/nginx/sbin/nginx -c /usr/local/webserver/nginx/conf/nginx.conf
144 pkill apache2
145 top
146 /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
147 /usr/local/webserver/nginx/sbin/nginx -c /usr/local/webserver/nginx/conf/nginx.conf
148 cd /home/webroot/
149 ls
150 nano index.php
151 ls
152 wget 192.168.1.100/linklink8.sql
153 wget 192.168.1.100/linklink8.tar.gz
154 tar zxvf linklink8.tar.gz
155 nano /usr/local/webserver/nginx/conf/nginx.conf
156 ls
157 clear
158 ls
159 cd linklink8
160 ls
161 top
162 ls
163 pkill nginx
164 /usr/local/webserver/nginx/sbin/nginx -c /usr/local/webserver/nginx/conf/nginx.conf
165 cd /home/webroot/
166 ls
167 cd ../
168 ls
169 cd nginx-php
170 ls
171 tar zxvf ZendOptimizer-3.3.0-linux-glibc21-i386.tar.gz
172 cd ZendOptimizer-3.3.0-linux-glibc21-i386
173 ls
174 install.sh
175 ./install.sh
176 pkill nginx
177 /usr/local/webserver/nginx/sbin/nginx -c /usr/local/webserver/nginx/conf/nginx.conf
178 pkill php-cgi
179 /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
180 /usr/local/webserver/nginx/sbin/nginx -c /usr/local/webserver/nginx/conf/nginx.conf
181 cd /home/webroot/linklink8/config/
182 ls
183 mysql -u root -p
184 mysql -u root -p -S /tmp/mysql.sock
185 mysql -u root -p -S /tmp/mysql.sock mydb < /home/webroot/mydb.sql
186 ls
187 nano db_config.php
188 cd ../
189 history >history_nginx.txt
下面为参考张宴文章。
安装步骤:
(系统要求:Linux 2.6+ 内核,本文中的Linux操作系统为CentOS 4.4)
一、获取相关开源程序:
1、下载程序源码包到当前目录:
本文中提到的所有开源软件为截止到2007年11月25日的最新稳定版。我将它们打了两个压缩包。
第一个压缩包:nginx-php-1.zip
下载地址:http://ishare.iask.sina.com.cn/cgi-bin/fileid.cgi?fileid=2746375
第二个压缩包:nginx-php-2.zip
下载地址:http://ishare.iask.sina.com.cn/cgi-bin/fileid.cgi?fileid=2746370
附:各开源软件单独下载地址(如果下载了以上两个压缩包,则无需下载以下软件):
http://down.s135.com/linux/nginx-php/ (登录用户名、密码均为blog.s135.com)
2、解压缩:
mkdir -p /data0/software
cd /data0/software
unzip nginx-php-1.zip
unzip nginx-php-2.zip
cd /data0/software/nginx-php
二、安装PHP 5.2.5(FastCGI模式)
1、编译安装PHP 5.2.5所需的支持库:
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.20.tar.gz
cd libpng-1.2.20/
./configure
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 ../
#—-If your system was install libxml2, you do not need to install it.—-
#tar zxvf libxml2-2.6.30.tar.gz
#cd libxml2-2.6.30/
#./configure
#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.6.tar.gz
cd mcrypt-2.6.6/
./configure
make
make install
cd ../
2、编译安装MySQL 5.0.45
/usr/sbin/groupadd mysql
/usr/sbin/useradd -g mysql mysql
tar zxvf mysql-5.0.45.tar.gz
cd mysql-5.0.45
./configure –prefix=/usr/local/webserver/mysql/ –without-debug –with-unix-socket-path=/tmp/mysql.sock –with-client-ldflags=-all-static –with-mysqld-ldflags=-all-static –enable-assembler –with-extra-charsets=gbk,gb2312,utf8 –with-pthread –enable-thread-safe-client
make && make install
chmod +w /usr/local/webserver/mysql
chown -R mysql:mysql /usr/local/webserver/mysql
cp support-files/my-medium.cnf /usr/local/webserver/mysql/my.cnf
cd ../
附:以下为附加步骤,如果你想在这台服务器上运行MySQL数据库,则执行以下两步。如果你只是希望让PHP支持MySQL扩展库,能够连接其他服务器上的MySQL数据库,那么,以下两步无需执行。
①、以mysql用户帐号的身份建立数据表:
/usr/local/webserver/mysql/bin/mysql_install_db –defaults-file=/usr/local/webserver/mysql/my.cnf –basedir=/usr/local/webserver/mysql –datadir=/usr/local/webserver/mysql/data –user=mysql –pid-file=/usr/local/webserver/mysql/mysql.pid –skip-locking –port=3306 –socket=/tmp/mysql.sock
②、启动MySQL(最后的&表示在后台运行)
/bin/sh /usr/local/webserver/mysql/bin/mysqld_safe –defaults-file=/usr/local/webserver/mysql/my.cnf &
3、编译安装PHP(FastCGI模式)
tar zxvf php-5.2.5.tar.gz
cd php-5.2.5/
./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 –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-force-cgi-redirect –enable-mbstring –with-mcrypt
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 ../
4、编译安装PHP5扩展模块
tar zxvf memcache-2.2.1.tgz
cd memcache-2.2.1/
/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.5/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 ../../../
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 = “gd.so”
自动修改:若嫌手工修改麻烦,可执行以下shell命令,自动完成对php.ini文件的修改:
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
6、创建www用户和组,以及其使用的目录:
/usr/sbin/groupadd www -g 48
/usr/sbin/useradd -u 48 -g www www
mkdir -p /data0/vshare/htdocs
chmod +w /data0/vshare/htdocs
chown -R www:www /data0/vshare/htdocs
7、安装lighttpd中附带的spawn-fcgi,用来启动php-cgi
注:压缩包中的spawn-fcgi程序为已经编译成二进制的版本。
cp spawn-fcgi /usr/local/webserver/php/bin
chmod +x /usr/local/webserver/php/bin/spawn-fcgi
8、启动php-cgi进程,监听127.0.0.1的10080端口,进程数为64(如果服务器内存小于3GB,可以只开启25个进程),用户为www:
/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
三、安装Nginx 0.5.33
1、安装Nginx所需的pcre库:
tar zxvf pcre-7.2.tar.gz
cd pcre-7.2/
./configure
make && make install
cd ../
2、安装Nginx
tar zxvf nginx-0.5.33.tar.gz
cd nginx-0.5.33/
./configure –user=www –group=www –prefix=/usr/local/webserver/nginx –with-http_stub_status_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
输入以下内容:
引用
user www www;
worker_processes 10;
error_log /data1/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 blog.s135.com;
index index.html index.htm index.php;
root /data0/vshare/htdocs;
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 /data1/logs/access.log access;
}
server
{
listen 80;
server_name status.blog.s135.com;
location / {
stub_status on;
access_log off;
}
}
}
②、在/usr/local/webserver/nginx/conf/目录中创建fcgi.conf文件:
vi /usr/local/webserver/nginx/conf/fcgi.conf
输入以下内容:
引用
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;
5、启动Nginx
ulimit -SHn 51200
/usr/local/webserver/nginx/sbin/nginx -c /usr/local/webserver/nginx/conf/nginx.conf
四、配置开机自动启动Nginx + PHP
vi /etc/rc.local
在末尾增加以下内容:
引用
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
五、优化Linux内核参数
vi /etc/sysctl.conf
在末尾增加以下内容:
引用
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
使配置立即生效:
/sbin/sysctl -p
六、在不停止Nginx服务的情况下平滑变更Nginx配置
(1)、修改/usr/local/webserver/nginx/conf/nginx.conf配置文件后,请执行以下命令检查配置文件是否正确:
/usr/local/webserver/nginx/sbin/nginx -t
如果屏幕显示以下两行信息,说明配置文件正确:
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
(2)、这时,输入以下命令查看Nginx主进程号:
ps -ef | grep “nginx: master process” | grep -v “grep” | awk -F ‘ ‘ ‘{print $2}’
屏幕显示的即为Nginx主进程号,例如:
6302
这时,执行以下命令即可使修改过的Nginx配置文件生效:
kill -HUP 6302