Ubuntu Server 16.04服务器版配置图解教程08 - nginx+php7.0基本配置
1、打开配置文件 /etc/nginx/nginx.conf:
sudo vim /etc/nginx/nginx.conf
配置是很容易理解 (你可以点击官方教程: http://wiki.nginx.org/NginxFullExample 或:http://wiki.nginx.org/NginxFullExample2)
首先(这是可选)调整keepalive_timeout到一个合理的值:
[...] keepalive_timeout 2; [...]
虚拟主机服务器{}容器定义。默认的虚拟主机是在文件中定义的/etc/nginx/sites-available/default
sudo vim /etc/nginx/sites-available/default
让我们来修改它,如下所示:
[...] server { listen 80 default_server; listen [::]:80 default_server; # SSL configuration # # listen 443 ssl default_server; # listen [::]:443 ssl default_server; # # Note: You should disable gzip for SSL traffic. # See: https://bugs.debian.org/773332 # # Read up on ssl_ciphers to ensure a secure configuration. # See: https://bugs.debian.org/765782 # # Self signed certs generated by the ssl-cert package # Don't use them in a production server! # # include snippets/snakeoil.conf; root /var/www/html; # Add index.php to the list if you are using PHP index index.html index.htm index.nginx-debian.html; server_name _; location / { # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. try_files $uri $uri/ =404; } # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ \.php$ { include snippets/fastcgi-php.conf; # 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; } # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # location ~ /\.ht { deny all; } } [...]
server_name _; 使这是一个默认捕捉所有虚拟主机(当然,你可以同时喜欢这里www.example.com指定主机名)。
根目录 /var/www/html;意味着文档根目录/var/www/html.
PHP的重要组成部分位置 ~ \.php$ {} stanza. 取消注释它来启用它。
现在保存文件并重新加载nginx:
sudo service nginx restart
下一步打开 /etc/php/7.0/fpm/php.ini…
sudo vim /etc/php/7.0/fpm/php.ini
设置 cgi.fix_pathinfo=0
[...] ; cgi.fix_pathinfo provides *real* PATH_INFO/PATH_TRANSLATED support for CGI. PHP's ; previous behaviour was to set PATH_TRANSLATED to SCRIPT_FILENAME, and to not grok ; what PATH_INFO is. For more information on PATH_INFO, see the cgi specs. Setting ; this to 1 will cause PHP CGI to fix its paths to conform to the spec. A setting ; of zero causes PHP to behave as before. Default is 1. You should fix your scripts ; to use SCRIPT_FILENAME rather than PATH_TRANSLATED. ; http://php.net/cgi.fix-pathinfo cgi.fix_pathinfo=0 [...]
重新加载 PHP-FPM:
sudo service php7.0-fpm restart
建立探针文件/var/www/html:
vim /var/www/html/info.php
<?php phpinfo(); ?>
浏览器访问 (e.g. http://192.168.1.**2/info.php):