Nginx是一个免费,开源,高性能的HTTP服务器。Nginx是其稳定性,功能集丰富,简单配置,资源消耗低。本教程演示如何在Fedora 16上安装PHP5+Nginx(PHP-FPM)+MySQL的服务器。
但是我不发出任何保证,它一定会正常工作!

1、初步说明
在本教程中使用的主机名与IP地址192.168.0.100 server1.example.com。这些设置可能会有所不同,所以你在适当情况下不得不更换他们。

2、安装的MySQL 5
首先,我们这样安装MySQL 5,终端输入命令:

   
   
   
   
  1. yum install mysql mysql-server

然后创建MySQL系统启动链接(这样MySQL在系统启动时自动启动)启动MySQL服务器:

 

   
   
   
   
  1. systemctl enable mysqld.service 
  2. systemctl start mysqld.service 

运行

   
   
   
   
  1. mysql_secure_installation 

配置mysql

   
   
   
   
  1. [root@server1 ~]# mysql_secure_installation 
  2.  
  3. NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL 
  4. SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY! 
  5.  
  6. In order to log into MySQL to secure it, we’ll need the current 
  7. password for the root user.  If you’ve just installed MySQL, and 
  8. you haven’t set the root password yet, the password will be blank, 
  9. so you should just press enter here. 
  10.  
  11. Enter current password for root (enter for none): <– ENTER 
  12. OK, successfully used password, moving on… 
  13.  
  14. Setting the root password ensures that nobody can log into the MySQL 
  15. root user without the proper authorisation. 
  16.  
  17. Set root password? [Y/n] <– ENTER 
  18. New password: <– 输入root的密码
  19. Re-enter new password: <– 重复输入root的密码
  20. Password updated successfully! 
  21. Reloading privilege tables.. 
  22. … Success! 
  23.  
  24. By default, a MySQL installation has an anonymous user, allowing anyone 
  25. to log into MySQL without having to have a user account created for 
  26. them.  This is intended only for testing, and to make the installation 
  27. go a bit smoother.  You should remove them before moving into a 
  28. production environment. 
  29.  
  30. Remove anonymous users? [Y/n] <– ENTER 
  31. … Success! 
  32.  
  33. Normally, root should only be allowed to connect from ’localhost’.  This 
  34. ensures that someone cannot guess at the root password from the network. 
  35.  
  36. Disallow root login remotely? [Y/n] <– ENTER 
  37. … Success! 
  38.  
  39. By default, MySQL comes with a database named ’test’ that anyone can 
  40. access.  This is also intended only for testing, and should be removed 
  41. before moving into a production environment. 
  42.  
  43. Remove test database and access to it? [Y/n] <– ENTER 
  44. - Dropping test database… 
  45. … Success! 
  46. - Removing privileges on test database… 
  47. … Success! 
  48.  
  49. Reloading the privilege tables will ensure that all changes made so far 
  50. will take effect immediately. 
  51.  
  52. Reload privilege tables now? [Y/n] <– ENTER 
  53. … Success! 
  54.  
  55. Cleaning up… 
  56.  
  57. All done!  If you’ve completed all of the above steps, your MySQL 
  58. installation should now be secure. 
  59.  
  60. Thanks for using MySQL! 
  61.  
  62. [root@server1 ~]# 

3、安装Nginx的
直接运行如下命令:

   
   
   
   
  1. yum install nginx 

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

   
   
   
   
  1. systemctl enable nginx.service 
  2. systemctl start nginx.service 

在您的Web服务器的IP地址或主机到浏览器(如http://localhost),你应该看到nginx的欢迎页面类型。

4、安装PHP5
需要安装一些列的模块,输入以下命令:

   
   
   
   
  1. yum install php-fpm php-cli php-mysql php-gd php-imap php-intl php-ldap php-odbc php-pear php-xml php-xmlrpc php-eaccelerator php-magickwand php-mbstring php-mcrypt php-mssql php-shout php-snmp php-soap php-tidy 

然后打开的/etc/php.ini并设定cgi.fix_pathinfo = 0:

   
   
   
   
  1. [...] 
  2. ; cgi.fix_pathinfo provides *real* PATH_INFO/PATH_TRANSLATED support for CGI. PHP’s 
  3. ; previous behaviour was to set PATH_TRANSLATED to SCRIPT_FILENAME, and to not grok 
  4. ; what PATH_INFO is. For more information on PATH_INFO, see the cgi specs. Setting 
  5. ; this to 1 will cause PHP CGI to fix its paths to conform to the spec. A setting 
  6. ; of zero causes PHP to behave as before. Default is 1. You should fix your scripts 
  7. ; to use SCRIPT_FILENAME rather than PATH_TRANSLATED. 
  8. ; http://www.php.net/manual/en/ini.core.php#ini.cgi.fix-pathinfo 
  9. cgi.fix_pathinfo=0 
  10. [...] 

然后我们最好还要修改以下我们的时区设置:

   
   
   
   
  1. cat /etc/sysconfig/clock 

得到我们的时区:

   
   
   
   
  1. [root@server1 ~]# cat /etc/sysconfig/clock 
  2. ZONE=”Asia/Shanghai” 
  3. [root@server1 ~]# 

下一步创建系统启动或者PHP-FPM并启动它:

   
   
   
   
  1. systemctl enable php-fpm.service 
  2. systemctl start php-fpm.service 

PHP-FPM是一个守护进程运行FastCGI服务器的端口9000。
5、nginx设置
输入命令:

   
   
   
   
  1. vi /etc/nginx/nginx.conf 

配置是很容易理解。
你可以再以下网址了解更多配置信息:

http://wiki.codemongers.com/NginxFullExample

http://wiki.codemongers.com/NginxFullExample2

按照以下配置设置值:

   
   
   
   
  1. [...] 
  2. worker_processes 4; 
  3. [...] 
  4. keepalive_timeout 2; 
  5. [...] 

虚拟主机是指在服务器{}容器。默认的虚拟主机是指在该文件 /etc/nginx/conf.d/default.conf -让我们作如下修改:

   
   
   
   
  1. [...] 
  2. server { 
  3. listen 80; 
  4. server_name _; 
  5.  
  6. #charset koi8-r; 
  7.  
  8. #access_log logs/host.access.log main; 
  9.  
  10. location / { 
  11. root /usr/share/nginx/html; 
  12. index index.php index.html index.htm; 
  13.  
  14. error_page 404 /404.html; 
  15. location = /404.html { 
  16. root /usr/share/nginx/html; 
  17.  
  18. # redirect server error pages to the static page /50x.html 
  19. error_page 500 502 503 504 /50x.html; 
  20. location = /50x.html { 
  21. root /usr/share/nginx/html; 
  22.  
  23. # proxy the PHP scripts to Apache listening on 127.0.0.1:80 
  24. #location ~ \.php$ { 
  25. # proxy_pass http://127.0.0.1; 
  26. #} 
  27.  
  28. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 
  29. location ~ \.php$ { 
  30. root /usr/share/nginx/html; 
  31. fastcgi_pass 127.0.0.1:9000; 
  32. fastcgi_index index.php; 
  33. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
  34. include fastcgi_params; 
  35.  
  36. # deny access to .htaccess files, if Apache’s document root 
  37. # concurs with nginx’s one 
  38. location ~ /\.ht { 
  39. deny all; 
  40. [...] 

保存和重启服务:

   
   
   
   
  1. systemctl reload nginx.service 

现在你可以建立一个探针文件进行测试

6、建立 PHP-FPM 使用一个 Unix Socket

   
   
   
   
  1. vi /etc/php-fpm.d/www.conf 
   
   
   
   
  1. [...] 
  2. ;listen = 127.0.0.1:9000 
  3. listen = /tmp/php5-fpm.sock 
  4. [...] 

重启PHP-FPM

   
   
   
   
  1. systemctl restart php-fpm.service 

配置文件:

   
   
   
   
  1. vi /etc/nginx/conf.d/default.conf 

配置内容如下:

   
   
   
   
  1. [...] 
  2. location ~ \.php$ { 
  3. root /usr/share/nginx/html; 
  4. fastcgi_pass unix:/tmp/php5-fpm.sock; 
  5. fastcgi_index index.php; 
  6. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
  7. include fastcgi_params; 
  8. [...] 

重启Nginx:

   
   
   
   
  1. service nginx restart