在Fedora 14中安装Nginx+MySQL+PHP5(PHP-FPM模式) LNMP

来源:互联网 发布:淘宝文案编辑软件 编辑:程序博客网 时间:2024/05/19 16:33

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

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

1 前言备注

在这篇教程中我使用的用户名是www.unixbar.net,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      1717/mysqld [root@server1 ~]#

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

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

并重启 MySQL 服务器:

/etc/init.d/mysqld restart

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

[root@server1 ~]# mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQLSERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY! In order to log into MySQL to secure it, we'll need the currentpassword for the root user.  If you've just installed MySQL, andyou haven't set the root password yet, the password will be blank,so you should just press enter here. Enter current password for root (enter for none): <--敲 ENTER键OK, successfully used password, moving on... Setting the root password ensures that nobody can log into the MySQLroot user without the proper authorisation. Set root password? [Y/n]<--敲 ENTER键 New password: <-- 输入你的密码Re-enter new password: <-- 重复输入你的密码Password updated successfully!Reloading privilege tables..... Success! By default, a MySQL installation has an anonymous user, allowing anyoneto log into MySQL without having to have a user account created forthem.  This is intended only for testing, and to make the installationgo a bit smoother.  You should remove them before moving into aproduction environment. Remove anonymous users? [Y/n] <--敲 ENTER键 ... Success! Normally, root should only be allowed to connect from 'localhost'.  Thisensures that someone cannot guess at the root password from the network. Disallow root login remotely? [Y/n] <--敲 ENTER键... Success! By default, MySQL comes with a database named 'test' that anyone canaccess.  This is also intended only for testing, and should be removedbefore moving into a production environment. Remove test database and access to it? [Y/n]<--敲 ENTER键 -        Dropping test database...... Success!- Removing privileges on test database...... Success! Reloading the privilege tables will ensure that all changes made so farwill take effect immediately. Reload privilege tables now? [Y/n] <--敲 ENTER键... Success! Cleaning up... All done!  If you've completed all of the above steps, your MySQLinstallation should now be secure. Thanks for using MySQL! [root@server1 ~]#

3安装 Nginx

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

yum install nginx

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

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

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

4 安装PHP5
Fedora 14官方的源中,并没有php-fpm,但是 我们可以使Remi repository源。使用下列命令添加Remi repository源

rpm -ivh http://rpms.famillecollet.com/remi-release-14.rpm

下面打开tc/yum.repos.d/remi.repo文件.

vi /etc/yum.repos.d/remi.repo

把[remi]部门的enabled改为1。Gpgcheck改为0.其他不需要修改

[remi]name=Les RPM de remi pour Fedora $releasever - $basearch#baseurl=http://rpms.famillecollet.com/fedora/$releasever/remi/$basearch/mirrorlist=http://rpms.famillecollet.com/fedora/$releasever/remi/mirrorenabled=1gpgcheck=0gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remifailovermethod=priority [remi-test]name=Les RPM de remi en test pour Fedora $releasever - $basearch#baseurl=http://rpms.famillecollet.com/fedora/$releasever/test/$basearch/mirrorlist=http://rpms.famillecollet.com/fedora/$releasever/test/mirrorenabled=0gpgcheck=1gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remifailovermethod=priority

现在我们安装php-cli和其他的PHP5模 块,例如php-mysql,它可以使你的PHP脚本支持MySQL:

yum install php-fpm 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-mbstring php-mcrypt php-mssql php-shout php-snmp php-soap php-tidy

然后打开/etc/php.ini文件,并解除注销cgi.fix_pathinfo=1这一行:

vi /etc/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://www.php.net/manual/en/ini.core.php#ini.cgi.fix-pathinfocgi.fix_pathinfo=1[...]

现在我们创建php-fpm的系统启动连接,并启动它:

chkconfig --levels 235 php-fpm on/etc/init.d/php-fpm start

Php-fpm是一个后台进程(初始化脚本为/etc/init.d/php-fpm),以9000端口运行在一台FastCGI服务器上.

5 配置nginx
现在我们打开nginx的配置文件/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 restart

现在在文档路径root /usr/share/nginx/html创建下列PHP文件:

vi /usr/share/nginx/html/info.php
<?phpphpinfo();?>

现在我们就可以在浏览器中通过http://192.168.0.100/info.php访 问了。

正如你在Server API这一行中所看到的一样,PHP5现在已经以FPM/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.unixbar.net/linux/fedora/1106.html


原创粉丝点击