Recommended VPS Complete list of best VPS hosting providers.

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

I installed Nginx, PHP5 with PHP-FPM, and also MySQL. So what’s next? Good question. What next you have to do is simply to setup Nginx virtual hosts file so you can host your site’s files and database in your server. Please remember, every time I say “server” here means to both VPS or Dedicated server. In this page I gonna show you how to add new website on your Nginx-based CentOS server for the very first time. The steps are pretty much similar if you want to repeat for adding another sites. One server is capable to hosts multiple sites with different domain and subdomain.

This tutorial is applicable for general types of websites but I will also post specific guide about how to add and install new WordPress-based blog in Nginx server.

Prerequisite

  1. A working CentOS server. In this guide I am using CentOS 6.3 x32 (see my playground server)
  2. Windows users should download Putty while Mac and Linux users can simply use Terminal
  3. You’ll also need a basic skill to use Putty and to navigate through SSH. Read: Most common Unix commands used to SSH.
  4. Follow my previous guide on how to install LNMP stack: Linux, Nginx, PHP and MySQL.
  5. About 15 minutes of your time and a cup of coffee if you like.

How to install NginX on CentOS

Step 1 – Login to your server and follow my previous guide about Basic setup for CentOS before you build a live web server. You may and may not follow that tutorial but if you followed, it will give you some basic security tweak to your server.

Before you proceed to the next steps, it is better to explain that all commands in this tutorial are written without the “sudo” prefix. However if you disabled root login and you logged in using another username with root privilege, you can add the “sudo” prefix all by your self. Alternatively you can simply type su, hit Enter and type in your password twice to switch login as root.

switch-root-login

You may also need to type this command to go to the root directory:

cd ~

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/

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

Note, its basic command syntax is:

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

 

But you can also use “public_html” as the folder instead of “htdocs”

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

Example:

make new directory

Step 3 – Then grant permissions for that newly created folder that acts as your website’s root directory. The default syntax is:

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

Replace “user” with username you are using to login to your server. Also replace “domain.com” with your own domain name. Example:

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

Step 4 – Also change permissions of the “/var/www” folder to 755:

chmod 755 /var/www

screenshot:

chmod 755

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. Read:Using SFTP To Upload and Manage Files On Your Server.

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

nano /var/www/domain.com/public_html/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.

Step 6 – This step is only necessary if your website’s scripts or CMS need MySQL database. What you have to do are: 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 7 – So here you come in the very important part of this tutorial: how to create virtual host file (aka server blocks) for new website in Nginx server. Unlike Nginx in Ubuntu that users can create separate server block for each domain, in CentOS the configuration for all domains stored in single “virtual.conf” file. So go open and edit it:

nano /etc/nginx/conf.d/virtual.conf

Step 8 – Nano editor will appear and you should see some default configurations. So go ahead edit it.

Default configuration (all lines are commented):

default server block

Now you can edit it as following (site with very default setup, no permalink or php caching integration):

server {
    listen       80;
#   listen       *:80;
    server_name  domain.com;

    location / {
        root   /var/www/domain.com/public_html/;
         index  index.html index.htm index.php;
    }
}

Example of mine:

nginx config example

Once done, hit Control+O to save then Control+X to exit.

Step 9 – Make sure the config you’ve just setup is alright and comply with standard Nginx syntax. How to test Nginx config? Simply issue this command:

nginx -t

It should return with something like this:

test nginx config

Step 10 – Finally restart Nginx with one of these command:

service nginx restart

or,..

/etc/init.d/nginx restart

Step 11 – Now you can test to access your server via your favorite web browser by typing your domain name. It should see like this:

Read: How to configure DNS records of your domain to resolve to your server.

success nginx setup

That’s it. You can also add more sites by repeating steps above. One thing you have to notice is in the step 8. You can add new configuration for the new site under the first configuration.

multisite conf

That’s it. Do not hesitate to drop comment below.

15 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.