lnmp环境搭建——nginx部分


1.安装依赖包

   
   
  1. yum install pcre-devel

2.下载并进入软件包


   
   
  1. wget http://nginx.org/download/nginx-1.6.3.tar.gz
  2. tar -xzvf nginx-1.6.3.tar.gz
  3. cd nginx-1.6.3

3.安装

   
   
  1. ./configure --user=www --group=www \
  2. --prefix=/usr/local/nginx16 \
  3. --sbin-path=/usr/local/nginx16/sbin/nginx \
  4. --conf-path=/usr/local/nginx16/etc/nginx.conf \
  5. --pid-path=/var/run/nginx16/nginx.pid \
  6. --lock-path=/var/lock/subsys/nginx.lock \
  7. --error-log-path=/var/log/nginx16/error.log \
  8. --http-log-path=/var/log/nginx16/access.log \
  9. --with-http_stub_status_module \
  10. --with-http_ssl_module \
  11. --with-http_gunzip_module \
  12. --with-http_gzip_static_module \
  13. --with-http_realip_module \
  14. --with-file-aio

查看是否安装成功

   
   
  1. /usr/local/nginx16/sbin/nginx -v
是否显示
   
   
  1. nginx version: nginx/1.6.3

启动nginx

   
   
  1. /usr/local/nginx16/sbin/nginx

查看是否成功启动
   
   
  1. curl localhost
若成功显示如下
    
    
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Welcome to nginx!</title>
  5. <style>
  6. body {
  7. width: 35em;
  8. margin: 0 auto;
  9. font-family: Tahoma, Verdana, Arial, sans-serif;
  10. }
  11. </style>
  12. </head>
  13. <body>
  14. <h1>Welcome to nginx!</h1>
  15. <p>If you see this page, the nginx web server is successfully installed and
  16. working. Further configuration is required.</p>
  17. <p>For online documentation and support please refer to
  18. <a href="http://nginx.org/">nginx.org</a>.<br/>
  19. Commercial support is available at
  20. <a href="http://nginx.com/">nginx.com</a>.</p>
  21. <p><em>Thank you for using nginx.</em></p>
  22. </body>
  23. </html>

nginx默认目录为 /usr/local/nginx16/html
现在如果在该目录下,添加demo.php
直接访问的话 
   
   
  1. curl localhost/demo.php
会直接jiangphp代码输出
   
   
  1. <?php
  2. phpinfo();
  3. ?>
下面配置nginx支持php

nginx支持php

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

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

即在server下添加
   
   
  1. location ~ \.php(.*)$ {
  2. fastcgi_pass 127.0.0.1:9000;
  3. fastcgi_index index.php;
  4. fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
  5. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  6. fastcgi_param PATH_INFO $fastcgi_path_info;
  7. fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
  8. include fastcgi_params;
  9. }

重启nginx
   
   
  1. /usr/local/nginx16/sbin/nginx -s reload

重新执行
   
   
  1. curl localhost/demo.php
成功执行。







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值