2
2

More than 5 years have passed since last update.

Setup nginx with virtual server on ubuntu

Posted at

Install nginx

$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt-get install nginx

Start nginx service

$ sudo service nginx start

Make sure nginx will start after server reboot

$ sudo update-rc.d nginx defaults

# should see sth like:
# System start/stop links for /etc/init.d/nginx already exist.

Configuration for virtual servers

Let's say there's an user called www-data for the websites directories, and nginx's default root directory is under /usr/share/nginx/www

These pre-conditions will be used in these steps below:

Create directory for your site

$ sudo mkdir -p /usr/share/nginx/www/your-site.com/pubic_html

This will create a directory for your-site.com domain and we will put all website files under it.

Change owner and permissions

$ sudo chown -R www-data:www-data /usr/share/nginx/www/your-site.com/pubic_html
$ sudo chmod 755 /usr/share/nginx/www

Create a simple index.html

In order to test our virtual host, we can create a simple index.html to see if it's works.

You can use any editor to do this, as long as you make sure index.html is placed under:/usr/share/nginx/www/your-site.com/pubic_html/index.html

Create the new virtual host configuration file

Nginx has provide a example configuration file, we can copy this:

$ cp /etc/nginx/sites-available/default /etc/nginx/sites-available/your-site.com

or, you can create a blank file and fill these content below in it:

your-site.com
server {
    listen      80; ## listen for ipv4; this line is default and implied
    #listen     [::]:80 default_server ipv6only=on; ## listen for ipv6

    root /usr/share/nginx/www/your-site.com;

    index index.html index.htm;

    server_name your-site.com;
}

Save and Exit.

Make a symbolic link

$ sudo ln -s /etc/nginx/sites-available/your-site.com /etc/nginx/sites-enabled/your-site.com

…and make sure to delete the default configuration file:

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

…after all, restart the nginx server:

$ sudo service nginx restart
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2