Gaurav Dave
Gaurav Dave

Reputation: 7474

Create Virtual Host on Nginx Server

I have been working a lot with LAMP lately, But now, I've started to work with nginx. So, I installed nginx and wanted to create virtual host, Since the folder structure of LAMP is different to that of stand alone nginx folder structure, and unable to understand how to create virtual host.

I have visited few links like:

  1. Tutorial 1, Tutorial 2 - Not Useful coz it is for standalone nginx

There's 1 Question on SO, it is also a kind of similar to my situation, but is unanswered.

Upvotes: 2

Views: 3099

Answers (3)

Gaurav Dave
Gaurav Dave

Reputation: 7474

Apache Virtual Host [/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 Virtual Host [/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;
    }
}

Once you update all the site information, you need to create a symbolic link to sites-enabled using sudo ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled/ then, reload your nginx using: sudo service nginx restart

Don't forget to add newly created site address in hosts [/etc/hosts] file.

If you're getting error which says "File Not Found." when you try to execute .php file, then it means that you need to change the user and group of php-fpm [/etc/php/7.0/fpm/pool.d/www.conf] file to your current user, and the restart php-fpm using: sudo /etc/init.d/php7.0-fpm restart

File References:

  • Nginx Config: /etc/nginx/nginx.conf
  • Nginx Error Log: /var/log/nginx/error.log

Upvotes: 0

justnajm
justnajm

Reputation: 4534

Define a file in your project directory for e.g. vhost.conf write following code:

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;
    }

}

include this file in: your_bitnami_install_directory/nginx/conf/bitnami/bitnami-apps-vhosts.conf

include "your_project_directory/vhost.conf";

restart the nginx

Upvotes: 1

dewaffled
dewaffled

Reputation: 2973

Default nginx config contains these lines to check sites-enabled directory:

http {
    # ...

    ##
    # Virtual Host Configs
    ##

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

I think you can edit config supplied by bitnami at /opt/bitnami/nginx/conf/nginx.conf to add these paths (or any other) and follow usual tutorials.

Upvotes: 1

Related Questions