在Debian Squeeze上安装Nginx+PHP5+MySQL

这是一篇纯粹为菜鸟谋福利的翻译教程(也是哥第一次装逼翻译)。为啥说是为菜鸟谋福利呢,因为老外写的教程通常都非常详细,每一步你需要做什么,为什么要这样做,会遇到什么问题都一一列了出来。所以,你懂的。

原文地址:

http://howtoforge.org/installing-nginx-with-php5-and-mysql-support-on-debian-squeeze

Tips:如果是想从Apache上迁移到Nginx只需要先停止apache服务
/etc/init.d/apache2 stop

然后卸载之
apt-get remove apache2

再然后略过以下已安装过软件步骤即可。

1、安装MySQL5

apt-get install mysql-server mysql-client

执行后进入安装,过程中会遇到MySQL提示为root用户设置密码:

New password for the MySQL “root” user: <– yourrootsqlpassword
Repeat password for the MySQL “root” user: <– yourrootsqlpassword

2、安装Nginx

Nginx已经被包含在了Debian Squeeze软件源中,所以我们只需要直接从源中安装:
apt-get install nginx

安装完成之后启动Nginx:
/etc/init.d/nginx start

Nginx默认的根目录是/var/www。如果没有这个目录,必须创建一个(并且设置目录所有者和用户组,以确保有权访问):

mkdir /var/www
chown www-data:www-data /var/www

在浏览器里面输入你服务器的IP或者域名(例如 http://192.168.0.100),你会看到与下图相似的页面:

因为/var/www下面没有索引页面,所以会出现以上403拒绝访问的错误提示。

4、安装PHP5

我们可以通过FastCGI让PHP5在Nginx中运行。很幸运,Debian Squeeze软件源中已经包含了一个PHP5的FastCGI模块包,所以我们只需要像这样子安装(其中包含了一些PHP的其它模块,例如php-mysql):
apt-get install php5-cgi php5-mysql php5-curl php5-gd php5-idn php-pear php5-imagick php5-imap php5-mcrypt php5-memcache 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 provides *real* PATH_INFO/PATH_TRANSLATED support for CGI. PHP’s
; previous behaviour was to set PATH_TRANSLATED to SCRIPT_FILENAME, and to not grok
; what PATH_INFO is. For more information on PATH_INFO, see the cgi specs. Setting
; this to 1 will cause PHP CGI to fix its paths to conform to the spec. A setting
; of zero causes PHP to behave as before. Default is 1. You should fix your scripts
; to use SCRIPT_FILENAME rather than PATH_TRANSLATED.
; http://php.net/cgi.fix-pathinfo
cgi.fix_pathinfo=1
[...]

因为Debian Squeeze上没有一个单独的FastCGI程序包,所以我们要用lighttpd的spawn-fcgi,安装lighttpd:
apt-get install lighttpd

安装后你会看到一个错误提示,告诉你因为80端口被占用所以lighttpd无法启动:

Starting web server: lighttpd2011-02-24 01:43:18: (network.c.358) can’t bind to port: 80 Address already in use
failed!
invoke-rc.d: initscript lighttpd, action “start” failed.

那是因为之前已经将Nginix启动,80端口被占用了。接着运行:
update-rc.d -f lighttpd remove

这样以后lighttpd就不会随开机启动了。

我们安装lighttpd只是因为要用它的spawn-gcgi(它的路径是/usr/bin/spawn-fcgi)。现在我们可以使用它了,输入以下命令:
spawn-fcgi --help

可以得到它的一些使用的相关说明。

启动一个以www-data的用户和用户组运行在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

当然了,谁都不想每次开机的时候都输入这么一长串的命令来启动一个程序。所以我们让它开机就自动启动。

打开/etc/rc.local
vi /etc/rc.local

然后将以上启动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
[...]

5、配置Nginx

Nginx的配置文件在/etc/nginx/nginx.conf 中,将它打开:
vi /etc/nginx/nginx.conf

配置文件非常容易理解,你可以去 http://wiki.codemongers.com/NginxFullExamplehttp://wiki.codemongers.com/NginxFullExample2 了解更多的内容。

首先将为worker processes添加一个数值(这一步是可选的),以及将keepalive_timeout设置为一个合量的值

[...]
worker_processes 5;
[...]
keepalive_timeout 2;
[...]

虚拟主机(vhost)被定义在server{}中。默认的虚拟主机配置文件路径是/etc/nginx/sites-available/default。我们来修改一下:
vi /etc/nginx/sites-available/default

[...]
server {

listen 80; ## listen for ipv4
listen [::]:80 default ipv6only=on; ## listen for ipv6

server_name _;

access_log /var/log/nginx/localhost.access.log;

location / {
root /var/www;
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$fastcgi_script_name;
include fastcgi_params;
}

# deny access to .htaccess files, if Apache’s document root
# concurs with nginx’s one
#
location ~ /\.ht {
deny all;
}
}
[...]

server_name _; 是指包含所有的虚拟主机(当然你也可以为它设置一个域名,例如www.example.com)。

在location /这部分当中,我在index这一行添加了index.php(即默认的索引文件)。以及root /var/www。意思是将/var/www设置为根目录。

最重要的是PHP相关的设置location ~ \.php$ {}。去掉这一部分的注释(即#号)来开启它。

确认将fastcgi_param这一行修改成fastcgi_param SCRIPT_FILENAME /var/www$fastcgi_script_name;(将/var/www替换成你自己设置的网站根目录)。

确认include和fastcgi_params;之间有几个空格。默认的配置文件中他们连在了一块,这是一个BUG。

好了,现在我们保存文件,并重启Nginx:
/etc/init.d/nginx restart

然后在/var/www下创建一个PHP文件:
vi /var/www/info.php

<?php phpinfo();

然后我们在浏览器中访问它(例如:http://192.168.0.100/info.php)

如你所见,在Server API一栏中显示PHP5是在FastCGI下运行的,另外下面的栏目中还会显示一些已经打开的PHP模块,包括MySQL模块。

Tags :

Google Reader Yahoo Facebook Twitter Digg FriendFeed Delicious Google Translate
这篇日志发表于2011年07月16日 03:35。 你可以订阅该日志的所有评论通过 RSS 2.0。 评论当前被关闭,你可以引用通告

3 条回应

评论(3)引用通告(0)

  1. 广哥

    很郁闷,看不明白。

    2011/07/19 23:45 | #1
  2. 包子

    那么Ubuntu下的安装方式也应该是差不多的吧

    2011/09/13 21:16 | #2
  3. ZZ

    几乎是一样吧

    2011/09/14 21:50 | #3