Although nginx prefers the term "Server Blocks"-- these are simply virtual hosts that allow users to run more than one website or domain off of a single VPS. Although we will be using nginx for this tutorial, for the purposes of tradition and easy comparison with Apache we can simply refer to them as virtual hosts.
The steps in this tutorial require the user to have root privileges on the virtual private server. You can see how to set that up in the Initial Server Setup Tutorial (steps 3 & 4). Furthermore, I will reference "user" throughout this tutorial-- feel free to replace this with any username you fancy.
You need to have nginx already installed on your VPS. If this is not the case, you can download it with this command:
sudo apt-get install nginx
*Notice: You will need to designate an actual DNS approved domain or IP address to test that a virtual host is working. Throughout this tutorial, I will simply use "example.com" to indicate when you should insert your correct domain name.
It is necessary to create a directory where you will keep the new website’s information. This location will be your Document Root in the Apache virtual configuration file later on.
By adding a -p to the line of code, the command automatically generates all the parents for the new directory.
sudo mkdir -p /var/www/example.com/public_html
It is important to remember to grant ownership of the directory to the right user. If you fail to do this, it will remain on the root system. Follow these commands to accomplish this:
sudo chown -R user:user /var/www/example.com/public_html
sudo chmod 755 /var/www
This will not only make sure ownership belongs to the correct user-- the second command also insures that everyone will be able to read your new files.
This tutorial will use nano to edit configuration files on your VPS. Typically, it is simpler to use than other text editors; however, if you prefer another such as vi, feel free to utilize whichever.
We need to create a new file called index.html within the directory we created earlier.
sudo nano /var/www/example.com/public_html/index.html
We can add some text to the file so we will have something to look at when the the site redirects to the virtual host.
<html> <head> <title>www.example.com</title> </head> <body> <h1>Success: You Have Set Up a Virtual Host</h1> </body> </html>
Save and Exit.
The next step is to create a new file that contains all of our virtual host information.
Conveniently, nginx provides us with a layout for this file in the sites-available directory (/etc/nginx/sites-available). All you need is to copy the text into a new custom file:
sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/example.com
Open up the new virtual host file— you will see all the information you need to set up virtual host within.
sudo nano /etc/nginx/sites-available/example.com
You'll need to make a few simple changes:
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; }
Save and Exit.
Finally, you'll need to activate the host by creating a symbolic link between the sites-available directory and the sites-enabled directory on your cloud server. This is an easy step to skip, so make sure to enter the following command:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/example.com
To avoid the "conflicting server name error" and ensure that going to your site displays the correct information, you can delete the default nginx server block:
sudo rm /etc/nginx/sites-enabled/default
We’ve made a lot of the changes to the configuration. Restart nginx and make the changes visible.
sudo service nginx restart
Once you have finished setting up your virtual host, type your domain name or IP address into the browser. It should display a message such as: Success-- You Have Set Up a Virtual Host.
Congratulations! Now to add additional virtual hosts on your cloud server, you can simply repeat the process above with a new document root/appropriate domaine name.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Perhaps consider using
$USER:$USER
inAnd why would you create
index.html
with sudo? you just gave the user ownership and write permissions.What about having multiple virtual hosts with nginx & php-fpm?
@Alex: Thanks, I’ve updated the article.
user-data:user-data should be www-data:www-data instead…fyi