Install Nginx with PHP 7.0 in Ubuntu 16.04

1.安装Nginx:

sudo apt-get update

sudo apt-get install nginx

2.安装php-fpm:

sudo apt-get install php-fpm

sudo nano /etc/php/7.0/fpm/php.ini    

修改被: cgi.fix_pathinfo = 0  

默认php在无法找到请求的php文件时,将尝试找到最近的文件执行,例如附件不允许发出请求的用户执行的脚本文件。

保存修改后,重启 : sudo systemctl restart php7.0-fpm

3.修改Nginx配置:

sudo nano /etc/nginx/sites-available/default

server {

listen 80 default_server;       //如果配置多个文件时,去掉default_server参数,它代表当所有虚拟主机都不匹配时,默认使用的主机。只能存在一个默认主机。

listen [::]:80 default_server;   //不用ipv6可以删除。

root /var/www/html;     //更改为入口脚本所在目录。

# Add index.php to the list if you are using PHP      添加入口脚本。

index index.php index.html index.htm index.nginx-debian.html;

server_name www.yourdomain.com;        //修改域名。

location / {

# First attempt to serve request as file, then

# as directory, then fall back to displaying a 404.

try_files $uri $uri/ =404;

修改为:try_files $uri $uri/ /index.php?$is_args$args;

}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

#

location ~ \.php$ {

include snippets/fastcgi-php.conf;     //引入fastcgi配置。

# With php7.0-cgi alone:

#fastcgi_pass 127.0.0.1:9000;

# With php7.0-fpm:       

fastcgi_pass unix:/run/php/php7.0-fpm.sock;    //使用php7.0-fpm时。

}

# deny access to .htaccess files, if Apache's document root

# concurs with nginx's one

#

location ~ /\.ht {            //添加git和svn :  location ~ /\.(ht|svn|git) {

deny all;

}

}

退出并保存修改。

sudo nginx -t      测试是否有语法错误。

sudo systemctl reload nginx  重启使设置生效。

4.测试

添加php文件到root。

sudo nano /var/www/html/info.php

写入info.php文件并保存。

pipinfo();

在浏览器输入http://server_name/info.php 。

如果成功应该会看到


Install Nginx with PHP 7.0 in Ubuntu 16.04_第1张图片

你可能感兴趣的:(Install Nginx with PHP 7.0 in Ubuntu 16.04)