首先請參照ubuntu14.04 lnmp(nginx1.9+php7.0+mysql5.7)此文,將ubuntu下的lnmp環境搭建好,當你這些準備工作都做好後,我們開始配置多域名,在本文中主要以Yii2為基礎展開配置。
修改nginx的配置檔案
#user如果不是www-data請修改為www-data
user www-data;
worker_processes auto;
pid /run/nginx.pid;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
#新增如下包含資料夾,易方便接下來的配置檔案統一管理和修改
include /etc/nginx/sites-enabled/*;
}
參照如上修改後,我們的配置檔案在 /etc/nginx/sites-enabled/
下面;
nginx預設的網站根目錄在/usr/share/nginx/html
,所以我們要新建資料夾並給他許可權,執行如下命令:
cd /var/www
如果不存在則用下面的命令:
sudo mkdir -p /var/www
虛擬主機配置如下
server {
listen 80 default_server; // 所有虛擬主機中只能有一個預設主機
charset utf-8;
client_max_body_size 128M;
index index.php index.html index.htm;
server_name localhost; # 域名 www.xxx.com
root /www # 專案檔案的入口資料夾
location / {
try_files $uri $uri/ /index.php?$args;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ .php$ {
include fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
location ~ /.(ht|svn|git) {
deny all;
}
}
啟動nginx
sudo nginx -s reload
如果啟動不成功,用如下命令檢查問題:
sudo nginx -t
根據提示除錯解決。
如果還不行,檢查 /etc/nginx/fastcgi_params ,下面詳細說明。
檢查fastcgi_params檔案
-
sudo vim /etc/nginx/fastcgi_params
-
檢視是否有如下程式碼:
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
沒有則加上。
提示
fastcgi_pass 127.0.0.1:9000; #也可以設定為這個,但要把 /etc/php5/fpm/pool.d 裡面的
listen = 127.0.0.1:9000
把下面這個語句註釋掉
; listen = /var/run/php5-fpm.sock 這個語句對應的是這個 fastcgi_pass unix:/var/run/php5-fpm.sock;