Skip to content

Instantly share code, notes, and snippets.

@nuhil
Last active August 29, 2015 14:16
Show Gist options
  • Save nuhil/98ed5744cd27fb571c9b to your computer and use it in GitHub Desktop.
Save nuhil/98ed5744cd27fb571c9b to your computer and use it in GitHub Desktop.
Nginx Create Virtual Host

Create Virtual Hosts in Nginx @Ubuntu

1. Setup


sudo apt-get install nginx

2. Create new directory


sudo mkdir -p /var/www/example.com/public_html

3. Give permissions


sudo chown -R www-data:www-data /var/www/example.com/public_html
sudo chmod 755 /var/www

4. Create test page


sudo nano /var/www/example.com/public_html/index.html

Content


<html>
  <head>
    <title>www.example.com</title>
  </head>
  <body>
    <h1>Success: You Have Set Up a Virtual Host</h1>
  </body>
</html>

5. Create new virtual host file


sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/example.com

6. Setup new virtual host


sudo nano /etc/nginx/sites-available/example.com

Content


 server {
        listen   80; ## listen for ipv4; this line is default and implied
        #listen   [::]:80 default ipv6only=on; ## listen for ipv6

        root /var/www/example.com/public_html;
        index index.html index.htm;

        # Make site accessible from http://localhost/
        server_name example.com;
}

7. Symlink to enable site


sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/example.com

8. Remove default file


sudo rm /etc/nginx/sites-enabled/default

9. Restart


sudo service nginx restart

10. Setup local hosts


nano /etc/hosts

# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1       localhost

#Virtual Hosts 
12.34.56.789    www.example.com 

Note: Tested in Ubuntu 12.04 LTS

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment