Chinaunix首页 | 论坛 | 博客
  • 博客访问: 424612
  • 博文数量: 161
  • 博客积分: 5005
  • 博客等级: 上校
  • 技术积分: 1090
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-20 16:38
文章分类

全部博文(161)

文章存档

2011年(21)

2010年(33)

2009年(89)

2008年(18)

我的朋友

分类: 系统运维

2011-03-18 01:37:41

(发音为 “engine x”)是一款免费、开源、高性能的HTTP服务器。同时Nginx以稳定、功能丰富、配置简单、资源消耗少著称。这篇教程将会为你展示如何在一台 Fedora 12中安装Nginx+MySQL+PHP5(FastCGI模式)

我已经测试无误,这将保证为你工作!

1 前言备注

在这篇教程中我使用的用户名是server1.example.com,IP地址是192.168.0.100.这些设置可能与你的有所不同,因此 你需要在适当的地方修改一下.

2.安装MySQL5

我们通过执行下面的命令来安装MySQL:

yum install mysql mysql-server

然后我们为MySQL创建系统启动连接(这样的话,MySQL就会在系统启动的时候自动启动)并且启动MySQL服务器:

chkconfig --levels 235 mysqld on
/etc/init.d/mysqld start

现在检查是否支持网络访问,运行:

netstat -tap | grep mysql

应该显示如下信息:

[root@server1 ~]# netstat -tap | grep mysql
tcp        0      0 *:mysql                     *:*                         LISTEN      1376/mysqld
[root@server1 ~]#

如果不显示,编辑/etc/my.cnf文件,并注释掉skip-networking参数:

vi /etc/my.cnf
[...]
 
#skip-networking
 
[...]

并重启 MySQL 服务器:

/etc/init.d/mysqld restart

运行

mysqladmin -u root password yourrootsqlpassword
mysqladmin -h server1.example.com -u root password yourrootsqlpassword

来为root用户设置一个密码(否则的话任何人都可以访问你的MySQL数据库!)。

3安装 Nginx

Nginx是Fedora12的默认包,我们可以通过下列命令安装它:

yum install nginx

然后我们为nginx创建一个系统启动链接,并启动它:

chkconfig --levels 235 nginx on
/etc/init.d/nginx start

在你的浏览器 中输入你的服务器IP地址或者主机名(例如),然后你就可以看到nginx的欢迎页面:

安装PHP5
我们可以让PHP5在nginx中以FastCGI的模式工作。默认情况下Fedora中没有独立的FastCGI deamon包,因此我们使用lighttpd的FastCGI包(lighttpd-FastCGI),并同时安装php-cli和其他的PHP5模 块,例如php-mysql,它可以使你的PHP脚本支持MySQL:

yum install lighttpd-fastcgi php-cli php-mysql php-gd php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-eaccelerator php-magickwand php-magpierss php-mapserver php-mbstring php-mcrypt php-mssql php-shout php-snmp php-soap php-tidy

然后打开/etc/php.ini文件,并在文件的最后加入这一行line cgi.fix_pathinfo = 1:

vi /etc/php.ini
[...]
cgi.fix_pathinfo = 1

我们可以使用Lighttpd-fastcgi包中自带的/usr/bin/spawn-fcgi,启动FastCGI进程。请参考

spawn-fcgi –help

来学习更多的东西.
我们运行下列命令可以启动一个监听本地9000端口,并以nginx用户和组运行的PHP FastCGI后台:

/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u nginx -g nginx -f /usr/bin/php-cgi -P /var/run/fastcgi-php.pid

当然,你并不像每次启动的时候都手动的输入这些命令,因此你可以让系统在启动时自动执行这些命令,打开/etc/rc.local…

vi /etc/rc.local

… 然后在文件的结尾添加下列命令:

[...]
/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u nginx -g nginx -f /usr/bin/php-cgi -P /var/run/fastcgi-php.pid

5 配置nginx

现在我们打开nginx的配置文件/etc/nginx/nginx.conf:

vi /etc/nginx/nginx.conf

配置文件简单易懂
(你可以在下列网站学习更多的配置方法和)
首先你可以增加worker process的数量和设置keepalive_timeout为一个合理值:

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

虚拟主机定义在server{}容器中.我们使用下列命令修改默认的虚拟主机:

[...]
    server {
        listen       80;
        server_name  _;
 
        #charset koi8-r;
 
        #access_log  logs/host.access.log  main;
 
        location / {
            root   /usr/share/nginx/html;
            index  index.php index.html index.htm;
        }
 
        error_page  404              /404.html;
        location = /404.html {
            root   /usr/share/nginx/html;
        }
 
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
 
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   
        #}
 
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html$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  ; 你可以在这里通过修改来确 定你的域名
在location /部分,我在index行加入了index.php。root /usr/share/nginx/html 意思是文档路径为/usr/share/nginx/html。
对于PHP来说最重要的部分就是 location ~ \.php$ {}。取消它的注释。改变root这一行为网站的文档路径。例如root /usr/share/nginx/html。请确保把fastcgi-param行修改成了fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;;否则得花PHP解析器将不会找到浏览器中调用的PHP.
现在我们保存文件并重启nginx:

/etc/init.d/nginx stop
/etc/init.d/nginx start

(我不能使用/etc/init.d/nginx restart,因为这个命令只停止了nginx,但是无法启动它-不知道为什么…)
现在在文档路径root /usr/share/nginx/html创建下列PHP文件:

vi /usr/share/nginx/html/info.php

现在我们就可以在浏览器中通过访 问了。

正如你在Server API这一行中所看到的一样,PHP5现在已经以FastCGI模式运行了。如果你继续向下翻看,你就能过看到PHP5所支持的模块,其中就包括 MySQL模块:

阅读(599) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~