?原由是因?yàn)橄胙a(bǔ)充點(diǎn)PHP方面的知識點(diǎn)剪芍,索性搭個學(xué)習(xí)的環(huán)境來學(xué)習(xí)學(xué)習(xí)雕蔽。
準(zhǔn)備:
需求:在ubuntu16.04環(huán)境下搭建nginx+php的學(xué)習(xí)環(huán)境
提示:由于我使用的是默認(rèn)的終端安裝歧斟,并沒有指定版本號闯团,所以會自動安裝默認(rèn)的版本函匕,安裝完成后的的版本是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的環(huán)境的搭建千诬,由于配置文件我不懂耍目,所以看到了一篇對口的
鏈接:配置鏈接
我來歸納一下基本步驟吧,怕萬一以后這個鏈接失效了就不好辦了
1.在nginx完成安裝后不出什么幺娥子的話你訪問localhost是能看到一個訪問頁面的徐绑,比如:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
2.接著開始配置agin的配置文件好讓nginx可以支持php(默認(rèn)配置文件在/etc/nginx/nginx.conf)
步驟1:打開配置文件sudo gedit /etc/nginx/nginx.conf
操作1:按照個人需求設(shè)定 keepalive_timeout 的值邪驮,比如我就設(shè)定的:keepalive_timeout? 2;
3.修改虛擬主機(jī)服務(wù)器{}容器定義,默認(rèn)的虛擬主機(jī)是在文件中定義的/etc/nginx/sites-available/default
步驟1:打開定義文件sudo gedit /etc/nginx/sites-available/default
操作1:把文件中的內(nèi)容修改為一下內(nèi)容:(從鏈接中直接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 默認(rèn)是www-data傲茄,如果要修改這個的話毅访,還要修改其他地方沮榜,我嫌麻煩,就先讓保持默認(rèn)
server_name _;使這是一個默認(rèn)捕捉所有虛擬主機(jī)(當(dāng)然喻粹,你可以同時喜歡這里www.example.com指定主機(jī)名)蟆融。
根目錄 /var/www/html;意味著文檔根目錄/var/www/html.
PHP的重要組成部分位置 ~ \.php$ {} 取消注釋它來啟用它。
現(xiàn)在保存文件并重新加載nginx:
sudo? service nginx reload
4.下一步打開/etc/php/7.0/fpm/php.ini(路徑根據(jù)版本號不同自行修改守呜,我的是php7.0)
sudo gedit? /etc/php/7.0/fpm/php.ini
設(shè)置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
內(nèi)容為:
<?php phpinfo();?>
保存后用瀏覽器訪問 (localhost/info.php):
要是出現(xiàn)在這個頁面就ok了:
ok型酥,環(huán)境基本就搭建好了,所需的數(shù)據(jù)庫還沒弄查乒,先到這兒冕末,水平有限,有什么誤人子弟的地方可以留言侣颂。