Nginx+php(FastCGI)+memcache+discuz安装
安装步骤:
(系统要求:Linux 2.6+ 内核,本文中的Linux操作系统为Rhel 4.)
一.获取相关开源程序:
PHP 5.2.6所需的支持库
下载地址:http://ishare.iask.sina.com.cn/cgi-bin/fileid.cgi?fileid=2746375
下载地址:http://ishare.iask.sina.com.cn/cgi-bin/fileid.cgi?fileid=2746370
Nginx:http://sysoev.ru/nginx/nginx-0.7.21.tar.gz
Php:http://cn.php.net/distributions/php-5.2.6.tar.gz
Mysql:http://down1.chinaunix.net/distfiles/mysql-5.0.56.tar.gz
2、解压缩:
#mkdir -p /software
#cd software
#unzip nginx-php.zip
#cd /software/nginx-php
#tar zxvf nginx-0.7.21.tar.gz
#tar zxvf php-5.2.6.tar.gz
#tar zxvf mysql-5.0.56.tar.gz
——————————————————————————–
二、安装PHP 5.2.6(FastCGI模式)
1、编译安装PHP 5.2.6所需的支持库:
#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
#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
#cd ../
#tar zxvf mcrypt-2.6.6.tar.gz
#cd mcrypt-2.6.6/
#./configure
#make
#make install
#cd ../
——————————————————————————–
2、编译安装MySQL 5.0.46
#/usr/sbin/groupadd mysql
#/usr/sbin/useradd -g mysql mysql
#tar zxvf mysql-5.0.46.tar.gz
#cd mysql-5.0.46
#./configure –prefix=/usr/local/mysql/ –with-unix-socket-path=/tmp/mysql.sock –without-debug –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
#cp support-files/my-medium.cnf /etc/my.cnf
#cd /usr/local/mysql
(1). 以mysql用户帐号的身份建立数据表:
#bin/mysql_install_db defaults-file=/etc/my.cnf –basedir=/usr/local/mysql –datadir=/usr/local/mysql/data –user=mysql –pid-file=/usr/local/mysql/mysql.pid –skip-locking –port=3306 –socket=/tmp/mysql.sock
#chown -R mysql:mysql /usr/local/mysql
(2).启动MySQL
#bin/mysqld_safe –user=mysql &
3.编译安装PHP-5.2.6
#export LD_PRELOAD=/usr/local/lib/preloadable_libiconv.so
#cd /share/nginx-php/php-5.2.6
#./configure –prefix=/usr/local/php –with-mysql=/usr/local/mysql/ –with-iconv=/usr/local/libiconv/lib
–with-libxml-dir –enable-xml –with-freetype-dir –with-jpeg-dir –with-png-dir –with-zlib –enable-fastcgi –with-mcrypt
#Make
#Make install
#cp php.ini-dist /usr/local/lib/php.ini
4、编译安装PHP5扩展模块
#tar zxvf memcache-2.2.1.tgz
#cd memcache-2.2.1/
#/usr/local/php/bin/phpize
#./configure –with-php-config=/usr/local/php/bin/php-config
#make
#make install
#cd /share/nginx-php/php-5.2.6/ext/gd/
#/usr/local/php/bin/phpize
#./configure –with-jpeg-dir –with-png-dir –with-zlib-dir –with-ttf –with-freetype-dir –with-php-config=/usr/local/php/bin/php-config
#make
#make install
5、修改php.ini文件
查找/usr/local /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”
6.创建www用户和组,以及其使用的目录:
#/usr/sbin/groupadd www -g 48
#/usr/sbin/useradd -u 48 -g www www
#mkdir -p / share/htdocs
#chmod +w /share/htdocs
#chown -R www:www vshare/htdocs
7、安装lighttpd中附带的spawn-fcgi,用来启动php-cgi
注:压缩包中的spawn-fcgi程序为已经编译成二进制的版本。
#cd /nginx/php/
#cp spawn-fcgi /usr/local/php/bin
#chmod +x /usr/local/php/bin/spawn-fcgi
8、启动php-cgi进程,监听127.0.0.1的10080端口,进程数为64(如果服务器内存小于3GB,可以只开启25个进程),用户为www,组为www:
#/usr/local/php/bin/spawn-fcgi -f /usr/local/php/bin/php-cgi -a 127.0.0.1 -p 10080 -C 25 -u www -g www
三、安装Nginx 0.5.33
1、安装Nginx所需的pcre库:
#tar zxvf pcre-7.2.tar.gz
#cd pcre-7.2/
#./configure
#make && make install
2、安装Nginx
#tar zxvf nginx-0.7.21.tar.gz
#cd nginx-0.7.21/
#./configure –user=www –group=www –prefix=/usr/local/webserver/nginx –with-http_stub_status_module
#make && make install
3.创建Nginx日志目录
#mkdir -p /share/logs
#chmod +w /share/logs
#chown -R www:www /share/logs
4.修改Nginx配置文件
修改添加以下内容:
————————————————————
user www www;
worker_processes 1;
error_log logs/error.log;
pid /var/run/nginx.pid;
events {
use epoll;
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main ‘$remote_addr – $remote_user [$time_local] “$request” ‘
‘”$status” $body_bytes_sent “$http_referer” ‘
‘”$http_user_agent” “$http_x_forwarded_for”‘;
server_names_hash_bucket_size 128;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
client_header_timeout 3m;
client_body_timeout 3m;
client_max_body_size 5m;
client_header_buffer_size 1k;
large_client_header_buffers 4 4k;
send_timeout 3m;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 8k;
gzip_http_version 1.1;
server {
listen 80;
server_name www.test5.com;
charset gb2312;
error_page 404 http://bbs.test5.com;
rewrite ^/bbs/(.*) http://bbs.test5.com/$1;
access_log logs/access.log main;
location / {
root /share/htdocs;
index index.php index.html;
}
if (-d $request_filename){
rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
}
error_page 404 /share/htdocs/404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /share/htdocs;
}
location = /ucenter/ {
alias /share/htdocs/vhost/;
index index.php index.html;
}
location ~ \.php$ {
root /share/htdocs;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fast-cgi.conf;
}
}
server
{
listen 80;
server_name status.test5.com;
location / {
stub_status on;
access_log off;
}
}
include vhost.conf;
}
————————————————————
5.编辑fast-cgi.conf
fastcgi_pass 127.0.0.1:10080;
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;
—————————————————————————————————————-
6.编辑Vhost.conf
server {
listen 80;
server_name bbs.test5.com;
index index.php index.html index.htm;
root /share/htdocs/vhost/bbs;
access_log /usr/local/nginx/logs/bbs/access.log;
error_log /usr/local/nginx/logs/bbs/error.log;
rewrite ^/archiver/((fid|tid)-[\w\-]+\.html)$ /archiver/index.php?$1;
rewrite ^/forum-([0-9]+)-([0-9]+)\.html$ /forumdisplay.php?fid=$1&page=$2;
rewrite ^/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ /viewthread.php?tid=$1&extra=page\%3D$3&page=$2;
rewrite ^/space-(username|uid)-(.+)\.html$ /space.php?action=viewpro&$1=$2;
rewrite ^/tag-(.+)\.html$ /tag.php?name=$1;
location ~ \.php$ {
include fast-cgi.conf;
}
location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico)$ {
access_log off;
}
}
server {
listen 80;
server_name blog.test5.com;
index index.php;
root /share/htdocs/vhost/home;
access_log /usr/local/nginx/logs/home/access.log;
error_log /usr/local/nginx/logs/home/error.log;
rewrite ^([0-9]+)/spacelist(.*)$ index.php?$1/action_spacelist$2;
rewrite ^([0-9]+)/viewspace_(.+)$ index.php?$1/action_viewspace_itemid_$2;
rewrite ^([0-9]+)/viewbbs_(.+)$ index.php?$1/action_viewbbs_tid_$2;
rewrite ^([0-9]+)/(.*)$ index.php?$1/$2;
rewrite ^([0-9]+)$ index.php?$1;
rewrite ^action_(.+)$ index.php?action_$1;
rewrite ^category_(.+)$ index.php?action_category_catid_$1;
rewrite ^itemlist_(.+)$ index.php?action_itemlist_catid_$1;
rewrite ^viewnews_(.+)$ index.php?action_viewnews_itemid_$1;
rewrite ^viewthread_(.+)$ index.php?action_viewthread_tid_$1;
rewrite ^index([\.a-zA-Z0-9]*)$ index.php;
rewrite ^html/([0-9]+)/viewnews_itemid_([0-9]+)\.html$ index.php?action_viewnews_itemid_$2;
rewrite ^/([0-9]+)/spacelist(.+)$ /index.php?uid/$1/action/spacelist/type$2;
rewrite ^/([0-9]+)/viewspace(.+)$ /index.php?uid/$1/action/viewspace/itemid$2;
rewrite ^/([0-9]+)/viewbbs(.+)$ /index.php?uid/$1/action/viewbbs/tid$2;
rewrite ^/([0-9]+)/(.*)$ /index.php?uid/$1/$2;
rewrite ^/([0-9]+)$ /index.php?uid/$1;
rewrite ^/action(.+)$ /index.php?action$1;
rewrite ^/category(.+)$ /index.php?action/category/catid$1;
rewrite ^/viewnews(.+)$ /index.php?action/viewnews/itemid$1;
rewrite ^/viewthread(.+)$ /index.php?action/viewthread/tid$1;
rewrite ^/mygroup(.+)$ /index.php?action/mygroup/gid$1;
location ~ \.php$ {
include fast-cgi.conf;
}
location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico)$ {
access_log off;
}
}
server {
listen 80;
server_name admin.test5.com;
index index.php;
root /share/htdocs/vhost/ucenter;
access_log /usr/local/nginx/logs/ucenter/access.log;
error_log /usr/local/nginx/logs/ucenter/error.log;
location ~ \.php$ {
include fast-cgi.conf;
}
}
四、配置开机自动启动Nginx + PHP
vi /etc/rc.local
在末尾增加以下内容:
引用
ulimit -SHn 51200
/usr/local/php/bin/spawn-fcgi -f /usr/local/r/php/bin/php-cgi -a 127.0.0.1 -p 10080 -C 64 -u www –u www
/usr/local//nginx/sbin/nginx -c /usr/local /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
六、在不停止Nginx服务的情况下平滑变更Nginx配置
(1)修改/usr/local/nginx/conf/nginx.conf配置文件后,请执行以下命令检查配置文件是否正确:
/usr/local/nginx/sbin/nginx –t
如果屏幕显示以下两行信息,说明配置文件正确:
the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
the configuration file /usr/local/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
3.重启php-cgi和nginx
(1)停止php-cgi和nginx:
killall php-cgi
kill -HUP `cat /var/run/nginx`
(2)启动php-cgi和nginx
/usr/local/php/bin/spawn-fcgi -f /usr/local/php/bin/php-cgi -a 127.0.0.1 -p 10080 -C 64 -u www –u www
/usr/local/nginx/sbin/nginx -c /usr/local /nginx/conf/nginx.conf
4.测试:
# vi /share/htdocs/index.php
IE地址栏: http://192.168.30.81/vhost/install/index.php
———————————————————-
七.安装Discuz论坛
1. UCenter、UCenter Home、Discuz! 集成安装包下载:
http://download.comsenz.com/UC_UCH_DZ/UC_UCH_DZ_SC_GBK.zip
将压缩包解压后的upload目录中的文件上传到服务器的/share/htdocs/vhost目录
2.安装配置
1.mysql数据库配置
(1)在mysql中创建一个论坛数据库discuz:
Mysql>create database discuz;
Mysql>use discuz;
Mysql>source /share/htdocs/vhost/bbs/install/discuz.sql; // 将discuz.sql导入,创建论坛
Mysql>grant create,drop,select,insert,update,delete on discuz.* to admin@localhost identified by “admin”; // 给discuz数据库创建一个用户
(2)配置/share/htdocs/vhost/bbs/config.in.php文件里的相关信息,
$dbhost = ‘localhost’; // 数据库服务器
$dbuser = ‘admin’; // 数据库用户名
$dbpw = ‘admin’; // 数据库密码
$dbname = ‘discuz’;
2.在浏览器中访问: http://192.168.30.81/vhost/install/index.php
3. 请根据向导,填入必要的信息,开始安装。
特别注意的是,要根据安装向导的提示,正确设置各个目录的读写属性。
4. 安装完毕后,为了数据安全,请务必将 install 目录删除。
5.安装完毕,拷贝以下信息:
UCenter的访问网址:
http://192.168.30.81/vhost/ucenter
UCenter 创始人密码:admin
DISCUZ!的访问网址:
http://192.168.30.81/vhost/bbs
管理员访问网址:
http://192.168.30.81/vhost/bbs/admincp.php
管理员帐号:admin 管理员密码:admin
UCenter Home的访问网址:
http://192.168.30.81/vhost/home
管理员访问网址:
http://192.168.30.81/vhost/home/admincp.php
管理员帐号:admin 管理员密码:admin
6. 在vhost.conf中设置相应的域名及访问路径即可对bbs、Ucenter、Ucenter Home
进行访问。
http://bbs.test5.,com
http://blog.test5.com
http://admin.test5.com
参考文章:http://wiki.codemongers.com/NginxChs
http://spaces.91book.cn/1/viewspace-8147