nginx的高效率是出了名了,最近想用它把服务器上的apache换掉,所以先在本地机上做了下测试。
下面为记录下的步骤!
第一步,安装mysql
sudo apt-get install mysql-server
第二步,安装php-cgi及所需gd库等
sudo apt-get install php5-cli php5-cgi php5-mysql php5-gd php5-mcrypt
第三步,安装nginx
sudo apt-get install nginx
第四步,从lighttpd中取得spawn-fcgi
apt-get install lighttpd
cp /usr/bin/spawn-fcgi ./
apt-get remove lighttpd
cp spawn-fcgi /usr/bin/
sudo vi /usr/bin/php-fastcgi
#!/bin/sh
/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -C 10 -u www-data -g www-data -f /u sr/bin/php-cgi
#注意 -C 10 表示开启10个php-cgi进程!
sudo chmod 755 /usr/bin/php-fastcg
sudo vi /etc/init.d/init-fastcgi
- #!/bin/bash
- PHP_SCRIPT=/usr/bin/php-fastcgi
- RETVAL=0
- case "$1" in
- start)
- $PHP_SCRIPT
- RETVAL=$?
- ;;
- stop)
- killall -9 php5-cgi
- RETVAL=$?
- ;;
- restart)
- killall -9 php5-cgi
- $PHP_SCRIPT
- RETVAL=$?
- ;;
- *)
- echo "Usage: php-fastcgi {start|stop|restart}"
- exit 1
- ;;
- esac
- exit $RETVAL
chmod 755 /etc/init.d/php-fastcgi
开启spawn-fcgi
/etc/init.d/php-fastcgi start
写入开机启动!
update-rc.d php-fastcgi defaults
查看php-cgi进程
ps ax | grep php5-cgi
第五步,配置nginx
sudo vi /etc/nginx/sites-enabled/default
server {
listen 80;
server_name localhost;
access_log /var/log/nginx/localhost.access.log;
location / {
root /home/xzy/www;
index index.html index.htm index.php;
}
location /doc {
root /usr/share;
autoindex on;
allow 127.0.0.1;
deny all;
}
location /images {
root /usr/share;
autoindex on;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/nginx-default;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
#proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#去除php支持的注释!
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
#为防止php.ini里的cgi.fix_pathinfo 不为1的话,就直接修下php脚本运行的路径!
fastcgi_param SCRIPT_FILENAME /home/xzy/www$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
# deny access to .htaccess files, if Apache’s document root
# concurs with nginx’s one
#开启支持.htaccess
location ~ /\.ht {
deny all;
}
#博客的图片较多,更改较少,将它们在浏览器本地缓存15天,可以提高下次打开我博客的页面加载速度。
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 15d;
}
}
sudo gedit /etc/nginx/fastcgi_params
最后一行注释掉
#fastcgi_param REDIRECT_STATUS 200;
重启nginx sudo /etc/init.d/nginx restart 看是否运行正常,正常的话,环境就算搭配成功了!
在/home/xzy/www上新建一个a.php测试下phpinfo是否正常!
vi /home/xzy/www/a.php
<?php phpinfo();?>
最后优化下nginx
vi /etc/nginx/nginx.conf
user www-data;
#修改nginx的进程数,每个进程占内存10~15m 按服务器配置和应用需求来设
worker_processes 1;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/access.log;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
tcp_nodelay on;
gzip on;
#开启并设置gzip配置 对网页文件、CSS、JS、XML等启动gzip压缩,减少数据传输量,提高访问速度
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}