原由是因为想补充点PHP方面的知识点,索性搭个学习的环境来学习学习。
准备:
需求:在ubuntu16.04环境下搭建nginx+php的学习环境
提示:由于我使用的是默认的终端安装,并没有指定版本号,所以会自动安装默认的版本,安装完成后的的版本是nginx1.10.× + php 7.0
要求:会打开终端
步骤:
步骤1:在终端输入:sudo apt-get update 更新一下
步骤2:在终端输入:sudo apt-get install nginx
步骤3:在终端输入:sudo apt-get install php
配置:
安装完成后是需要额外的配置nginx的配置文件才能完成nginx+php的环境的搭建,由于配置文件我不懂,所以看到了一篇对口的
链接:配置链接
我来归纳一下基本步骤吧,怕万一以后这个链接失效了就不好办了
1.在nginx完成安装后不出什么幺娥子的话你访问localhost是能看到一个访问页面的,比如:
2.接着开始配置agin的配置文件好让nginx可以支持php(默认配置文件在/etc/nginx/nginx.conf)
步骤1:打开配置文件sudo gedit /etc/nginx/nginx.conf
操作1:按照个人需求设定 keepalive_timeout 的值,比如我就设定的:keepalive_timeout 2;
3.修改虚拟主机服务器{}容器定义,默认的虚拟主机是在文件中定义的/etc/nginx/sites-available/default
步骤1:打开定义文件sudo gedit /etc/nginx/sites-available/default
操作1:把文件中的内容修改为一下内容:(从链接中直接copy过来)
[...]
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;
}
}
[...]
修改的部分操作解释为:
user 默认是www-data,如果要修改这个的话,还要修改其他地方,我嫌麻烦,就先让保持默认
server_name _;使这是一个默认捕捉所有虚拟主机(当然,你可以同时喜欢这里www.example.com指定主机名)。
根目录 /var/www/html;意味着文档根目录/var/www/html.
PHP的重要组成部分位置 ~ \.php$ {} 取消注释它来启用它。
现在保存文件并重新加载nginx:
sudo service nginx reload
4.下一步打开/etc/php/7.0/fpm/php.ini(路径根据版本号不同自行修改,我的是php7.0)
sudo gedit /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 reload
建立探针文件/var/www/html:
sudo gedit /var/www/html/info.php
内容为:
<?php phpinfo();?>
保存后用浏览器访问 (localhost/info.php):
要是出现在这个页面就ok了:
ok,环境基本就搭建好了,所需的数据库还没弄,先到这儿,水平有限,有什么误人子弟的地方可以留言。