Recommended VPS Complete list of best VPS hosting providers.

How To Add New Site Into Your Nginx-based Ubuntu Server

I told you already about how to install essential “things” on your Ubuntu server (also works on Debian) to build a live server to host your websites, forums or blogs. Those “things” I am talking about here are Nginx which is a world’s most popular web server , MySQL, PHP and Postfix. With those “things” installed, you are already having all necessary stuff and you can now put your website’s files and database in it. But wait, you will still need to know where your files should be located / stored in your server, how to make your website accessible, and also how to install fresh CMS script (eg; WordPress) on it.

Are you curious? Read on!!

p.s: When I say “server” it refers to both VPS or Dedicated one.

p.p.s: This guide will also answer your question about how to setup Nginx virtual hosts file to add new site in Ubuntu server.

Prerequisite

  1. A server running either Ubuntu or Debian. In this example I use Ubuntu 12.04.
  2. You’ll need either Putty in Windows or Terminal in Mac and Linux to access your server via SSH connection.
  3. In this example I will use Putty. If you do so, make sure you know where to download it and how to use it. Read: Using Putty To SSH To Your Server From Windows.
  4. You have to also know all basic commands useful to SSH-ing your server. Read: Most common Unix command to manage unmanaged server.
  5. Make sure you have installed: Nginx, PHP, MySQL, and Postfix.
  6. You may also setup Bind9 DNS software first or may be later after you add your website into your server.

The How To Steps

Step 1 – Login to your server via SSH connection. If you didn’t change it yet, it should be in port 22 otherwise use your defined port. If you did follow my previous guide about all essential basic configuration for Ubuntu server, then you should login using your username instead of root. Once logged in, type su, hit Enter on your keyboard then type in your root password.

login ssh

Step 2 – Next, you will need to create the directory where you will and can put all the files of your website in it. When I say “website” here is a single website with single domain. I will talk about multiple domain later (please remind me if I forget). To do that, use following command:

mkdir -p /var/www/domain.com/htdocs/ /var/www/domain.com/logs/

Please replace domain.com with your own (e.g: servermom.com).

Note, its basic command syntax is:

mkdir -p /var/www/domain.com/htdocs/

mkdir htdocs

but you may also create “logs” folder to store all the log files for future reviewing. Also, you can also use “public_html” as the folder instead of “htdocs”

mkdir -p /var/www/domain.com/public_html/

Step 4 – Now grant permissions and navigate to that folder:

Use following command to grant permission:

chown -R www-data:www-data /var/www/domain.com/

or,..

chown -R www:www /var/www/domain.com/public_html
chmod 755 /var/www

Replace “www:www” to username you are using. In my case I use “sawiyati:sawiyati” so the command will be:

chown -R sawiyati:sawiyati /var/www/domain.com/public_html

One done, go to that folder..

cd /var/www/domain.com/htdocs/

or,..

cd /var/www/domain.com/public_html/

chown htdocs

That’s it. Now you can place all your website’s or blog’s files there. There are two most quick ways to do that: Firstly by zipping your files and folders from your computer into a single .zip file then upload it to another host that supports direct link and finally download it to your server using “wget” command then finally unzip it. Secondly, you can install FTP server software in your Ubuntu server then use FTP client app like FileZilla in your computer. Well, we will discuss that in separate article in the future.

Just for testing purpose, you can simply create a test index.html file in there:

nano index.html

nano index html

Then use this or use your own (just for testing):

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

Do not forget to hit Control+O to save then Control+X to exit Nano editor.

default index file

Step 5 – Create new MySQl database, add new MySQL user and apply necessary privilege to that new user to grant access to the database. Once done import your database if necessary then update your website script configuration so it can connect to the database. Read: Most common and basic MySQL commands.

Step 6 – Next, you’ll need to setup virtual hosts file or also known as server blocks. It is easy, simply use this command:

vim /etc/nginx/sites-available/domain.com

or use Nano directly

nano /etc/nginx/sites-available/domain.com

edit virtual hosts

The simple text editor screen will appear. You may see all the information you need to set up virtual host within.

Step 7 – You will need to adjust, edit, setup and define your own configurations within. What configs you should use depends on which script(s) you are using. Below is the very basic virtual hosts file config:

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

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

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

Remember to change domain.com with your own and replace “public_html” with “htdocs” if you prefer to use htdocs instead of public_html folder.

virtual hosts file

For WordPress users, below is the most standard virtual hosts entries. What I mean “standard” here without any other plugin that alter .htaccess (in Aphace) file (e.g: caching plugin like W3TC and WP-Supercache)

server {
        server_name domain.com www.domain.com;

	access_log   /var/log/nginx/domain.com.access.log;
	error_log    /var/log/nginx/domain.com.error.log;

        root /var/www/domain.com/public_html;
        index index.php;

        location / {
                try_files $uri $uri/ /index.php?$args; 
        }

        location ~ \.php$ {
                include fastcgi_params;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
        }
}

Step 8 – Next, you have to also activate the host by creating a symbolic link between the sites-available directory and the sites-enabled directory. Shortly, you have to add domain.com to Nginx sites-enabled folder. Use this command:

ln -s /etc/nginx/sites-available/domain.com /etc/nginx/sites-enabled/

copy virtual hosts

Step 9 – Alright, before we restart Nginx, make sure the virtual hosts file configuration is correct. You can perform the test by typing this command:

nginx -t

test nginx config

Step 10 – Restart Nginx so all what you’ve configured can be loaded fully. This command syntax may be useful:

service nginx reload

or,..

service nginx restart

nginx restart

That is it. You can now open your favorite web browser and access your website in it. If everything goes well, you’ll see your website there or at least the index.html test file you created earlier.

index html test file

Optional

As what I said in step 2 above, you may also create “logs” directory in where all the log files will be stored. If you decided to do so, perform these commands as well:

ln -s /var/log/nginx/domain.com.access.log /var/www/domain.com/logs/access.log

and this one:

ln -s /var/log/nginx/domain.com.error.log /var/www/domain.com/logs/error.log

Q: I want to add another new website with different domain name, what should I do?
A: Simply follow the whole steps again.

That’s it and that’s it. Any question? Drop via comment form below. Any way, that’s it for now but I will obviously explain any other things in the near future. Thanks..

5 Comments

Add a Comment

Your email address will not be published. Required fields are marked *

Get more stuff like this
in your inbox

Subscribe and get interesting stuff plus faster updates to your email.