前置環(huán)境
- ubuntu 16
- nginx 1.10.3
- PHP7.0
- mysql
安裝nginx
sudo apt-get install nginx
如何查看安裝目錄侦另?
which nginx
# 方法一
whereis nginx
# 方法二
配置適用于php的nginx
安裝完成后驱入,在nginx目錄下找到nginx.conf,這個是它的默認(rèn)配置文件蟹略。可擴(kuò)展遏佣,閱讀源碼你會發(fā)現(xiàn)更多挖炬!
# 一般web服務(wù)配置
server {
# web服務(wù)監(jiān)聽端口
listen 8080;
# web使用域名或ip
server_name 192.168.1.125;
# web訪問路徑
root /var/www/html;
# 配置規(guī)則
location / {
index index.html index.htm index.php l.php;
#是否打開目錄瀏覽
autoindex off;
# rewrite 重寫
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=/$1 last;
break;
}
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# 設(shè)定正向代理
#
#location /www/api {
# proxy_pass http://127.0.0.1; #代理IP
# proxy_set_header Host $host; # 代理網(wǎng)站域名 $host=server_name
# proxy_set_header X-Real-IP $remote_addr;
# proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
# 設(shè)定編譯并識別 php文件 ,包括開啟pathinfo(此項(xiàng)受安裝版本影響)
location ~ \.php(.*)$ {
# fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
}
}
訪問PHP文件状婶,必須配置好php-fpm文件 意敛, 找到php-fpm配置文件 /etc/php5/fpm/pool.d/www.conf 并打開修改 里面listen = /var/run/php5-fpm.sock 為 listen = 9000;
安裝php7
apt-get install php7.0
apt-get install php7.0-fpm
php -v #查看是否安裝成功
查看php是否能運(yùn)行成功,編輯 index.php
<?php phpinfo(); ?>
查看php運(yùn)行狀態(tài)
systemctl status php-fpm
提示:
重新加載 PHP-FPM:service php7.0-fpm reload
安裝php支持庫
- apt-get -y install php7.0-mysql (php連接mysql膛虫,包含PDO等 )
- apt-get -y install php7.0-curl ( php端遠(yuǎn)程請求庫 )
---------此處是php5版本
---------安裝 php5-fpm 模塊
apt-get install php5-cgi
apt-get install php5-fpm
apt-get install php5-curl (客戶端 URL 庫函數(shù)庫)
apt-get install php5-gd (GD庫圖像函數(shù)庫)
apt-get install php5-mysql (增加對mysql支持)
apt-get install php5-mcrypt (Mcrypt 加密函數(shù)庫)
apt-get install php5-memcache (Memcache客戶端)
apt-get install php5-memcached (Memcache服務(wù)端)
apt-get install php5-dev (開發(fā)支持模塊)
配置開啟 Gzip
- 壓縮大幅提高頁面加載速度
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
#gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
gzip_vary off;
gzip_disable "MSIE [1-6]\.";
說明:
第1行:開啟Gzip
第2行:不壓縮臨界值草姻,大于1K的才壓縮,一般不用改
第3行:buffer稍刀,一般不用改
第4行:用了反向代理的話撩独,末端通信是HTTP/1.0,有這句的話注釋了就行了账月,默認(rèn)是HTTP/1.1
第5行:壓縮級別综膀,1-10,數(shù)字越大壓縮的越好局齿,時間也越長剧劝,看心情隨便改吧
第6行:進(jìn)行壓縮的文件類型,缺啥補(bǔ)啥就行了抓歼,JavaScript有兩種寫法担平,最好都寫上吧,總有人抱怨js文件沒有壓縮锭部,其實(shí)多寫一種格式就行了
第7行:跟Squid等緩存服務(wù)有關(guān)暂论,on的話會在Header里增加"Vary: Accept-Encoding",我不需要這玩意拌禾,自己對照情況看著辦吧
第8行:IE6對Gzip不怎么友好取胎,不給它Gzip了
nginx 緩存過期配置
expire參數(shù)接受的值可以是:
- expires 1 January, 1970, 00:00:01 GMT; //設(shè)定到具體的時間
- expires 60s; //60秒
- expires 30m; //30分鐘
- expires 24h; //24小時
- expires 1d; //1天
- expires max; //max表示過期時間為31 December 2037 23:59:59 GMT
- expires off; //永遠(yuǎn)重新讀取
安裝Mysql
apt-get –y install mysql-server # 此命令僅安裝mysql服務(wù),支持php需安裝php7.0-mysql
apt-get –y install mysql-client # 此命令安裝mysql客戶端,如類似phpAdmin用于管理mysql的