北京SEO

lnmp环境配置之安装配置Nginx与PHP教程

2019/10/10/17:45:59  阅读:1793  来源:谷歌SEO算法  标签: Safari浏览器

lnmp环境中nginx与php是一个重头戏了,很多朋友在配置这一步时都要折腾很多,在此小编也同样是折腾了,下面我整理了一篇lnmp环境中安装配置Nginx与PHP教程,希望例子可以帮助到大家.

安装Nginx的方式有很多种,这里我们还是编译源码进行安装,使用下列命令:

  1. $wgethttp://nginx.org/download/nginx-1.6.2.tar.gz
  2. $tar-zxvfnginx-1.6.2.tar.gz
  3. $cdnginx-1.6.2
  4. $./configure--prefix=/usr/local/nginx
  5. $make
  6. $sudomakeinstall

如果安装过程中出现如下错误:

  1. ./configure:error:theHTTPrewritemodulerequiresthePCRElibrary.
  2. Youcaneitherdisablethemodulebyusing--without-http_rewrite_module
  3. option,orinstallthePCRElibraryintothesystem,orbuildthePCRElibrary
  4. staticallyfromthesourcewithnginxbyusing--with-pcre=<path>option.

则需要先安装pcre:

$ sudo yum install pcre-devel

安装完成之后,我们的Nginx安装目录在/usr/local/nginx,接下来修改nginx的配置文件(/usr/local/nginx/conf/nginx.conf),使其能够处理php脚本.

  1. worker_processes1;
  2. events{
  3. worker_connections1024;
  4. }
  5. http{
  6. includemime.types;
  7. default_typeapplication/octet-stream;
  8. sendfileon;
  9. keepalive_timeout65;
  10. server{
  11. listen80;
  12. server_name_;
  13. root/vagrant;
  14. location/{
  15. indexindex.htmlindex.htmindex.php;
  16. }
  17. location/demo{
  18. indexindex.php;
  19. if(!-e$request_filename){
  20. rewrite^/demo/(.*)$/demo/index.php?$1last;
  21. break;
  22. }
  23. }
  24. error_page500502503504/50x.html;
  25. location=/50x.html{
  26. roothtml;
  27. }
  28. location~\.php${
  29. fastcgi_pass127.0.0.1:9000;
  30. fastcgi_indexindex.php;
  31. fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name;
  32. includefastcgi_params;
  33. }
  34. }
  35. }

最后,启动Nginx时,需要先启动PHP-FPM.

  1. $sudo/usr/local/php/sbin/php-fpm
  2. $sudo/usr/local/nginx/sbin/nginx

对于Nginx的重启以及关闭操作,可以使用以下命令.

$ sudo /usr/local/nginx/sbin/nginx -s [reload|restart|stop]

而PHP-FPM,则麻烦一点,需要先使用ps -ef|grep php-fpm获取master process的进程ID,再使用kill -USR2:

  1. $ps-ef|grepphp-fpm
  2. root62211002:17?00:00:00php-fpm:masterprocess(/usr/local/php/etc/php-fpm.conf)
  3. nobody62226221002:17?00:00:00php-fpm:poolwww
  4. nobody62236221002:17?00:00:00php-fpm:poolwww
  5. vagrant62331623002:18pts/000:00:00grepphp-fpm
  6. $sudokill-USR26221

注意:-USR2参数为重启,-INT参数为关闭.

创建虚拟主机:

  1. [root@os11728httpd-2.2.22]#vi/usr/local/nginx/conf/vhosts/www_finet230_cn.conf

内容如下:

  1. server{
  2. listen8080;
  3. server_nameng.fine230.cnfinet85.cn;
  4. root/var/www/root/ng_finet230_cn;
  5. #激活/关闭自动索引
  6. autoindexon;
  7. #设定索引时文件大小的单位(B,KB,MB或GB)
  8. #默认为on,显示出文件的确切大小,单位是bytes。
  9. #改为off后,显示出文件的大概大小,单位是kB或者MB或者GB
  10. autoindex_exact_sizeoff;
  11. #开启以本地时间来显示文件时间的功能。默认为关(GMT时间)
  12. #默认为off,显示的文件时间为GMT时间。
  13. #改为on后,显示的文件时间为文件的服务器时间
  14. autoindex_localtimeon;
  15. #charsetkoi8-r;
  16. location/{
  17. indexindex.htmlindex.htmindex.php;
  18. }
  19. #error_page404/404.html;
  20. #redirectservererrorpagestothestaticpage/50x.html
  21. #
  22. error_page500502503504/50x.html;
  23. location=/50x.html{
  24. root/var/www/root/ng_finet230_cn;
  25. }
  26. #proxythePHPscriptstoApachelisteningon127.0.0.1:80
  27. #
  28. #location~\.php${
  29. #proxy_passhttp://127.0.0.1;
  30. #}
  31. #passthePHPscriptstoFastCGIserverlisteningon127.0.0.1:9000
  32. #
  33. #location~\.php${
  34. #roothtml;
  35. #fastcgi_pass127.0.0.1:9000;
  36. #fastcgi_indexindex.php;
  37. #fastcgi_paramSCRIPT_FILENAME/scripts$fastcgi_script_name;
  38. #includefastcgi_params;
  39. #}
  40. #denyaccessto.htaccessfiles,ifApache’sdocumentroot
  41. #concurswithnginx’sone
  42. #
  43. #location~/\.ht{
  44. #denyall;
  45. #}
  46. #将客户端的请求转交给fastcgi
  47. location~.*\.(php|php5|shtml)?${
  48. #roothtml;
  49. fastcgi_pass127.0.0.1:9000;#这里指定了fastcgi进程侦听的端口,nginx就是通过这里与php交互的
  50. fastcgi_indexindex.php;
  51. fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name;
  52. includefastcgi_params;
  53. }
  54. #网站的图片较多,更改较少,将它们在浏览器本地缓存30天
  55. location~.*\.(gif|jpg|jpeg|png|bmp|swf)$
  56. {
  57. expires30d;
  58. }
  59. #网站会加载很多JS、CSS,将它们在浏览器本地缓存1小时
  60. location~.*\.(js|css)?$
  61. {
  62. expires1h;
  63. }
  64. }
  65. #anothervirtualhostusingmixofIP-,name-,andport-basedconfiguration
  66. #
  67. #server{
  68. #listen8000;
  69. #listensomename:8080;
  70. #server_namesomenamealiasanother.alias;
  71. #location/{
  72. #roothtml;
  73. #indexindex.htmlindex.htm;
  74. #}
  75. #}
  76. #HTTPSserver
  77. #
  78. #server{
  79. #listen443;
  80. #server_namelocalhost;
  81. #sslon;
  82. #ssl_certificatecert.pem;
  83. #ssl_certificate_keycert.key;
  84. #ssl_session_timeout5m;
  85. #ssl_protocolsSSLv2SSLv3TLSv1;
  86. #ssl_ciphersHIGH:!aNULL:!MD5;
  87. #ssl_prefer_server_cipherson;
  88. #location/{
  89. #roothtml;
  90. #indexindex.htmlindex.htm;
  91. #}//phpfensi.com
  92. #}
  93. server
  94. {
  95. listen8080;
  96. server_namestatus.ng.finet230.cn;
  97. location/{
  98. stub_statuson;
  99. access_logoff;
  100. }
  101. }

将/var/www/root/ng_finet230_cn目录下的所有档案与子目录的拥有者皆设为www群体的使用者www:

[root@os11728 ~]# chown -R www:www /var/www/root/ng_finet230_cn

1.5.Nginx的启动与关闭

启动Nginx:

  1. [root@os11728~]#ulimit-SHn65535
  2. root@os11728~]#/usr/local/nginx/sbin/nginx

停止Nginx:

  1. [root@os11728~]#/usr/local/nginx/sbin/nginx-sstop
  2. //或
  3. [root@os11728~]#/usr/local/nginx/sbin/nginx-squit

重启Nginx:

  1. [root@os11728~]#/usr/local/nginx/sbin/nginx-sreload
  2. //或
  3. [root@os11728~]#kill-HUP`cat/usr/local/nginx/logs/nginx.pid`

配置开机自动启动Nginx + PHP,代码如下:

[root@os11728 ~]# vi /etc/rc.local

在末尾增加以下内容:

  1. ulimit-SHn65535
  2. /usr/local/php/sbin/php-fpm
  3. /usr/local/nginx/sbin/nginx

广告内容

lnmp环境配置之安装配置Nginx与PHP教程 lnmp环境配置之安装配置Nginx与PHP教程 lnmp环境配置之安装配置Nginx与PHP教程

相关阅读

热门评论

SEO探索者团队 SEO探索者团队

SEO服务&网站优化

总篇数182

精选文章

RMAN中catalog和nocatalog区别介绍 小技巧:为Linux下的文件分配多个权限 zimbra8.5.1安装第三方签名ssl证书的步骤 解决mysql不能远程连接数据库方法 windows服务器mysql增量备份批处理数据库 mysql中slow query log慢日志查询分析 JavaScript跨域问题总结 Linux下负载均衡软件LVS配置(VS/DR)教程 mysql中权限参数说明 MYSQL(错误1053)无法正常启动

SEO最新算法