ubuntu14.04 lnmp nginx 虚拟主机(多站点 多域名) 配置

carolAnn 2019-06-20

首先请参照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;

相关推荐