[linux][web server]从Apache 转移到 Nginx 的操作笔记
2010年9月12日星期日 | | | RedHat AS4 U5 +nginx-0.7.30 + php-5.2.8 + mysql-5.1.30 +Zend-3.3.3 + vsftpd-2.0.5
作者:Linuxsun
出处:LSUN技术论坛 http://bbs.linuxsun.cn
目标:运用低的硬件配置做最高效的服务平台!
本次实验:
P4 2.8 512M内存 80G硬盘 10M独享 nginx虚拟机数量5个以上 vsftpd二个虚拟用户 域名:
http://bbs.linuxsun.cn
基本软件的安装,文章可以看:http://bbs.linuxsun.cn/search.php?
searchid=6&;orderby=lastpost&ascdesc=desc&searchsubmit=yes&page=2
这里主要说明一些出错的解决方法以及给出源配置文件。
基本的软件都运行起来了:
======================================================
[root@localhost ~]# netstat -na |grep LISTEN
tcp 0 0 0.0.0.0:903 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:21 0.0.0.0:* LISTEN
tcp 0 0 221.5.41.5:53 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:53 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:953 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
tcp 0 0 :::22 :::* LISTEN
======================================================
Nginx.conf
-------------------------------------------------------------
user www linuxsun;
worker_processes 10;
pid /var/log/nginx.pid;
error_log /dev/null;
worker_rlimit_nofile 51200;
events
{
use epoll;
#maxclient = worker_processes * worker_connections / cpu_number
worker_connections 51200;
}
http
{
include /usr/local/nginx/conf/mime.types;
default_type application/octet-stream;
charset gb2312;
log_format main '$remote_addr - $remote_user [$time_local] $request '
'"$status" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /dev/null;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
ignore_invalid_headers on;
recursive_error_pages on;
server_name_in_redirect off;
sendfile on;
keepalive_timeout 60;
tcp_nopush on;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 128k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
fastcgi_intercept_errors on;
client_max_body_size 50m;
client_body_buffer_size 256k;
gzip on;
gzip_min_length 0;
gzip_buffers 16 8k;
gzip_http_version 1.0;
gzip_comp_level 9;
gzip_types text/plain text/css image/x-icon image/png;
gzip_vary on;
proxy_temp_path /dev/shm/proxy_temp;
fastcgi_temp_path /dev/shm/fastcgi_temp;
client_body_temp_path /dev/shm/client_body_temp;
#www.linuxsun.cn
include /usr/local/nginx/conf/vhosts/linuxsun.conf;
}
-------------------------------------------------------------
/usr/local/nginx/conf/vhosts/linuxsun.conf
============================================
server {
listen 80;
server_name bbs.linuxsun.cn;
root /www/bbs;
location / {
index index.html index.htm index.php;
root /www/bbs;
rewrite ^(.*)/archiver/((fid|tid)-[\w\-]+\.html)$ $1/archiver/index.php?$2 last;
rewrite ^(.*)/forum-([0-9]+)-([0-9]+)\.html$ $1/forumdisplay.php?fid=$2&page=$3 last;
rewrite ^(.*)/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ $1/viewthread.php?
tid=$2&extra=page%3D$4&page=$3 last;
rewrite ^(.*)/profile-(username|uid)-(.+)\.html$ $1/viewpro.php?$2=$3 last;
rewrite ^(.*)/space-(username|uid)-(.+)\.html$ $1/space.php?$2=$3 last;
rewrite ^(.*)/tag-(.+)\.html$ $1/tag.php?name=$2 last;
}
location ~ \.(png|jpg|gif|bmp|swf)$ {
proxy_store on;
access_log off;
proxy_store_access user:rw group:rw all:rw;
}
location ~ \.php$ {
include /usr/local/nginx/conf/fscgi.conf;
}
}
server {
listen 80;
server_name sns.linuxsun.cn;
root /www/sns;
location / {
index index.html index.htm index.php;
rewrite ^(.*)/archiver/((fid|tid)-[\w\-]+\.html)$ $1/archiver/index.php?$2 last;
rewrite ^(.*)/forum-([0-9]+)-([0-9]+)\.html$ $1/forumdisplay.php?fid=$2&page=$3 last;
rewrite ^(.*)/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ $1/viewthread.php?
tid=$2&extra=page%3D$4&page=$3 last;
rewrite ^(.*)/profile-(username|uid)-(.+)\.html$ $1/viewpro.php?$2=$3 last;
rewrite ^(.*)/space-(username|uid)-(.+)\.html$ $1/space.php?$2=$3 last;
rewrite ^(.*)/tag-(.+)\.html$ $1/tag.php?name=$2 last;
}
location ~ \.(png|jpg|gif|bmp|swf)$ {
proxy_store on;
access_log off;
proxy_store_access user:rw group:rw all:rw;
}
location ~ \.php$ {
include /usr/local/nginx/conf/fscgi.conf;
}
}
server {
listen 80;
server_name ebak.linuxsun.cn;
root /www/ebak;
location / {
index index.html index.htm index.php;
}
location ~ \.(png|jpg|gif|bmp|swf)$ {
access_log off;
proxy_store on;
proxy_store_access user:rw group:rw all:rw;
}
location ~ \.php$ {
include /usr/local/nginx/conf/fscgi.conf;
}
}
server {
listen 80;
server_name ftp.linuxsun.cn;
root /www/ftp/cnc;
location / {
autoindex on;
index index.html index.htm index.php;
}
location ~ \.(png|jpg|gif|bmp|swf)$ {
proxy_store on;
access_log off;
proxy_store_access user:rw group:rw all:rw;
}
location ~ \.php$ {
include /usr/local/nginx/conf/fscgi.conf;
}
}
=============================================
/usr/local/nginx/conf/fscgi.conf
----------------------------------------
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.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;
fastcgi_param HTTP_ACCEPT_ENCODING gzip,deflate;
fastcgi_param HTTP_ACCEPT_ENCODING attach;
fastcgi_param HTTP_ACCEPT_ENCODING rar,deflate;
----------------------------------------
vsftpd安装配置可以看:http://bbs.linuxsun.cn/search.php?
searchid=7&;orderby=lastpost&ascdesc=desc&searchsubmit=yes
/etc/vsftpd.conf
---------------------------------------
# Example config file /etc/vsftpd.conf
anonymous_enable=NO
local_enable=YES
user_config_dir=/etc/vsftpd/user_list
guest_enable=YES
#guest_username=daemon
#pam_service_name=vsftpd
pam_service_name=/etc/pam.d/ftp.vu
connect_from_port_20=YES
write_enable=YES
local_umask=000
dirmessage_enable=YES
xferlog_enable=YES
listen=YES
listen_port=21
------------------------------------
虚拟用户linuxsun
------------------------------------------
[root@localhost ~]# cat /etc/vsftpd/user_list/linuxsun
local_umask=077
guest_username=linuxsun
anon_upload_enable=YES
anon_mkdir_write_enable=YES
anon_other_write_enable=YES
local_root=/www/ftp/cnc
-----------------------------------
有一个非常郁闷的问题。LeapFTP 用linuxsun登录,上传一个文件测试下。发现权限-rw-----
根据网上的资料说,设置成:
local_umask=000
但是我自己测试,就算这样设置,新上传的文件权限还是-rw-----
why????
算了,用crontab 来自动设置权限吧:
================================
#[root@localhost ~]# crontab -l
3 * * * * /bin/rndc reload 1> /dev/null
10 6 6 * * /usr/sbin/ntpdate 210.72.145.44
0-1 * * * * /bin/chmod -R 777 /www/ftp
================================
如果此处权限不是777的话,Discuz!附件去上传不了。下载也不行。
php 和mysql的配置文件就不用贴了。。。
基本不做修改。。。
通过以上配置文件,一个高速、稳定的平台出来了。
看下机器的使用情况:
top - 18:20:27 up 1 day, 5:56, 1 user, load average: 0.00, 0.00, 0.00
Tasks: 74 total, 2 running, 72 sleeping, 0 stopped, 0 zombie
Cpu(s): 0.0% us, 0.3% sy, 0.0% ni, 99.7% id, 0.0% wa, 0.0% hi, 0.0% si
Mem: 498900k total, 427408k used, 71492k free, 149696k buffers
Swap: 1052248k total, 0k used, 1052248k free, 109176k cached
一般会出现的问题:
1. [url=viewthread.php?tid=672&highlight=nginx]No input file specified[/url]
2.[url=viewthread.php?tid=740&highlight=nginx]proxy_pass后图片不能显示![/url]
3. discuz!开启全部伪静态,首页能显示,贴子无法查看。
我完全是从LAMP 移过来的,感觉速度和性能都提高几倍。
在网上翻了不少资料,喜欢的朋友欢迎转载,在此感谢CU 51cto "NetSeek"