美文网首页
Ubuntu Nginx中搭建php环境

Ubuntu Nginx中搭建php环境

作者: 康熙微博私访记 | 来源:发表于2016-05-22 23:52 被阅读904次

Ubuntu Nginx中搭建php环境

这篇文章主要介绍了Ubuntu中搭建Nginx、PHP环境最简单的方法,本文讲解的是使用apt-get工具安装Nginx、PHP环境,并讲解了基本配置,需要的朋友可以参考下。

  1. 系统环境:Ubuntu 14.14 64位
  2. PHP环境:PHP Version 5.5.9

配置步骤

安装php5-fpm

sudo apt-get install nginx php5-fpm

编辑nginx配置文件

vi /etc/nginx/sites-enabled/default

在default配置文件中添加php配置,此处不修改默认配置,采用其他端口的站点,在default中添加如下代码:

server {
    listen 8088 default_server;
    listen [::]:8080 default_server ipv6only=on;

    root /alidata/www/mywebsite;
    index index.html index.htm index.php;

    # Make site accessible from http://localhost/
    server_name localhost;

    location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            try_files $uri $uri/ =404;
            # Uncomment to enable naxsi on this location
            # include /etc/nginx/naxsi.rules
    }

    location ~ \.php$ {
            # fastcgi_split_path_info ^(.+\.php)(/.+)$;
            # # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
            #
            # # With php5-cgi alone:
            # fastcgi_pass 127.0.0.1:9000;
            # # With php5-fpm:
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
    }
}

需要注意的几个地方:

  1. 在server中需要添加index.php

  2. 如下代码为php的location配置:

     location ~ \.php$ {
             # fastcgi_split_path_info ^(.+\.php)(/.+)$;
             # # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
             #
             # # With php5-cgi alone:
             # fastcgi_pass 127.0.0.1:9000;
             # # With php5-fpm:
             fastcgi_pass unix:/var/run/php5-fpm.sock;
             fastcgi_index index.php;
             include fastcgi_params;
     }
    

重新启动Nginx

sudo service nginx restart

测试配置

在mywebsite目录中添加测试php文件index.php

<?php
    echo phpinfo();
?>

在浏览器中输入http://localhost:8080/index.php 即可看到php的信息,表示配置成功。

相关文章

网友评论

      本文标题:Ubuntu Nginx中搭建php环境

      本文链接:https://www.haomeiwen.com/subject/mmharttx.html