中国共有多少个万达:在Fedora 11中安装Nginx+MySQL+PHP5(FastCGI...
来源:百度文库 编辑:偶看新闻 时间:2024/05/03 06:08:28
Nginx(发音为 “engine x”)是一款免费、开源、高性能的HTTP服务器。同时Nginx以稳定、功能丰富、配置简单、资源消耗少著称。这篇教程将会为你展示如何在一台Fedora 11中安装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 mysqltcp 0 0 *:mysql *:* LISTEN 1702/mysqld[root@server1 ~]#
如果不显示,编辑/etc/my.cnf文件,并注释掉skip-networking参数:
vi /etc/my.cnf[...] #skip-networking [...]
并重启 MySQL 服务器:
/etc/init.d/mysqld restart
运行
mysqladmin -u root password yourrootsqlpasswordmysqladmin -h server1.example.com -u root password yourrootsqlpassword
来为root用户设置一个密码(否则的话任何人都可以访问你的MySQL数据库!)。
3安装 Nginx
Nginx是Fedora11的默认包,我们可以通过下列命令安装它:
yum install nginx
然后我们为nginx创建一个系统启动链接,并启动它:
chkconfig --levels 235 nginx on/etc/init.d/nginx start
在你的浏览器 中输入你的服务器IP地址或者主机名(例如http://192.168.0.100),然后你就可以看到nginx的欢迎页面:
4 安装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-mhash 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的配置文件in /etc/nginx/nginx.conf:
vi /etc/nginx/nginx.conf
配置文件简单易懂
(你可以在下列网站学习更多的配置方法http://wiki.codemongers.com/NginxFullExample 和http://wiki.codemongers.com/NginxFullExample2)
首先你可以增加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 http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ \.php$ { root /usr/share/nginx/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 www.unixbar.net; 你可以在这里通过修改www.unixbar.net来确定你的域名
在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
现在我们就可以在浏览器中通过http://192.168.0.100/info.php访问了。
正如你在Server API这一行中所看到的一样,PHP5现在已经以FastCGI模式运行了。如果你继续向下翻看,你就能过看到PHP5所支持的模块,其中就包括MySQL模块:
6 相关链接
- nginx: http://nginx.net/
- nginx Wiki: http://wiki.codemongers.com/Main
- PHP: http://www.php.net/
- MySQL: http://www.mysql.com/
- Fedora: http://fedoraproject.org/
7 原文地址
http://www.howtoforge.com/installing-nginx-with-php5-and-mysql-support-on-fedora-11