Nginx是一个免费、开源、高性能的HTTP服务器。Nginx以其稳定的性能、丰富的功能、简单的配置、低资源消耗而闻名。此教程帮助你在Ubuntu下安装支持PHP和MySQL的nginx服务器。
1、安装前注意事项
首先Ubuntu的软件安装要用root权限,所以命令前要全部加Sudo,然后输入密码。否则会显示权限不够。
其次安装前最好先运行apt-get update更新本地软件。以免安装过程中出现错误。
2、安装MySQL5.0
运行语句
apt-get install mysql-server mysql-client
安装过程中会让要求输入MySQL的root用户密码
New password for the MySQL “root” user:
运行
apt-get install nginx
启动nginx
/etc/init.d/nginx start
访问你服务器会看到nginx欢迎界面
将nginx加入开机启动
update-rc.d nginx defaults
4、安装PHP5
PHP5通过FastCGI在nginx下运行。乌班图提供一个FastCGI-enabled PHP5 安装包,可以这样安装。
apt-get install php5-cgi php5-mysql php5-curl php5-gd php5-idn php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-mhash php5-ming php5-pspell php5-recode php5-snmp php5-sqlite php5-tidy php5-xmlrpc php5-xsl
打开 /etc/php5/cgi/php.ini 配置文件,在最后一行添加cgi.fix_pathinfo = 1
vi /etc/php5/cgi/php.ini
[…]
cgi.fix_pathinfo = 1
Ubuntu没有独立的FastCGI安装包,所以用lighttpd里面的spawn-fcgi,运行下面命令:
apt-get install lighttpd
安装完成时会出现lighttpd无法启动的错误,因为nginx占用了80端口。运行
update-rc.d -f lighttpd remove
使lighttpd开机不启动。
我们安装lighttpd只需要其中的/usr/bin/spawn-fcgi,来运行FastCGI进程。运行
spawn-fcgi –help
查看它的命令帮助。
以用户www-data在本机localhost的9000端口下运行一个PHP FastCGI进程,输入以下命令
/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u www-data -g www-data -f /usr/bin/php5-cgi -P /var/run/fastcgi-php.pid
加入开机运行,以免每次开机运行此命令。
vi /etc/rc.local
在最后一行加入下面语句(在exit前面)。
[…]
/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u www-data -g www-data -f /usr/bin/php5-cgi -P /var/run/fastcgi-php.pid
[…]
5、nginx配置
编辑配置文件
vi /etc/nginx/nginx.conf
首先(可选) 增加worker processes 的数量,设置keepalive_timeout 到适当的值:
[…]
worker_processes 5;
[…]
keepalive_timeout 2;
[…]
接着配置虚拟主机,默认虚拟主机路径是/etc/nginx/sites-available/default
vi /etc/nginx/sites-available/default
[…]
server {
listen 80;
server_name _;
access_log /var/log/nginx/localhost.access.log;
location / {
root /var/www/nginx-default;
index index.php index.html index.htm;
}
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
#
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/nginx-default$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
# deny access to .htaccess files, if Apache’s document root
# concurs with nginx’s one
#
location ~ /\.ht {
deny all;
}
}
[…]
可以直接复制过去,以免修改过程中出现错误。
修改好了,重启nginx
/etc/init.d/nginx restart
在默认目录/var/www/nginx-default建立PHP文件info.php
vi /var/www/nginx-default/info.php
phpinfo();
?>