在Nginx服务器上创建虚拟主机

时间:2015-06-30 06:16:25

标签: nginx server virtualhost

我最近和LAMP一直在合作,但是现在,我已经开始使用nginx了。所以,我安装了nginx并想创建虚拟主机,因为LAMP的文件夹结构不同于独立的nginx文件夹结构,而且无法理解如何创建虚拟主机。

我访问了几个链接,如:

  1. Tutorial 1Tutorial 2 - 无用,因为它适用于独立的nginx
  2. 1 Question on SO,它也有类似于我的情况,但未答复

3 个答案:

答案 0 :(得分:1)

默认nginx配置包含这些行以检查启用站点的目录:

http {
    # ...

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

我认为您可以在/opt/bitnami/nginx/conf/nginx.conf编辑bitnami提供的配置,以添加这些路径(或任何其他路径),并按照常规教程进行操作。

答案 1 :(得分:1)

在项目目录中定义一个文件,例如vhost.conf 写下面的代码:

server {

    listen    8080;
    root   "your_project_directory";
    server_name  your_host_name;

    index index.php;
    allow 127.0.0.1;
    deny all;

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_read_timeout 300;
        fastcgi_pass unix:/**your_bitnami_install_directory**/php/var/run/www.sock;
        fastcgi_index index.php;
        fastcgi_param  SCRIPT_FILENAME $request_filename;
        include fastcgi_params;
    }

}

将此文件包含在:your_bitnami_install_directory / nginx / conf / bitnami / bitnami-apps-vhosts.conf

include "your_project_directory/vhost.conf";

重新启动nginx

答案 2 :(得分:0)

  

Apache虚拟主机[ /etc/apache2/sites-available/000-default.conf ]

<VirtualHost *:8080>
  ServerName abc.dev
  DocumentRoot "/home/gauravdave01/Development/project001/source/public"
    <Directory /home/gauravdave01/Development/project001/source/public>
        DirectoryIndex index.php
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

<VirtualHost *:8080>
  ServerName mno.dev
  DocumentRoot "/home/gauravdave01/Development/project002/source/public"
    <Directory /home/gauravdave01/Development/project002/source/public>
        DirectoryIndex index.php
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>
  

Nginx虚拟主机[ / etc / nginx / sites-available / default ]

server {
    listen 80 default_server;

    root /usr/share/nginx/example.com;
    index index.php index.html index.htm;

    server_name example.com www.example.com;
    location / {
        try_files $uri $uri/ /index.php;
    }

    location ~ \.php$ {
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        include snippets/fastcgi-php.conf;
    }
}

server {
    root /home/gauravdave01/Development/sample.org;
    index index.php index.html index.htm;

    server_name sample.org www.sample.org;
    location / {
        try_files $uri $uri/ /index.php;
    }

    location ~ \.php$ {
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        include snippets/fastcgi-php.conf;
    }
}

更新所有网站信息后,您需要使用sudo ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled/创建指向已启用网站的符号链接,然后使用以下内容重新加载您的nginx:sudo service nginx restart

不要忘记在主机[ / etc / hosts ]文件中添加新创建的网站地址。

如果您收到错误消息“未找到文件”。当你尝试执行.php文件时,那意味着你需要更改php-fpm的用户和组[ /etc/php/7.0/fpm/pool.d/www.conf ]将文件发送给当前用户,并使用:sudo /etc/init.d/php7.0-fpm restart

重新启动php-fpm
  

文件参考:

  • Nginx配置: /etc/nginx/nginx.conf
  • Nginx错误日志: /var/log/nginx/error.log