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
- A working CentOS server. In this guide I am using CentOS 6.3 x32 (see my playground server)
- Windows users should download Putty while Mac and Linux users can simply use Terminal
- You’ll also need a basic skill to use Putty and to navigate through SSH. Read: Most common Unix commands used to SSH.
- Follow my previous guide on how to install LNMP stack: Linux, Nginx, PHP and MySQL.
- 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.
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:
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:
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):
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:
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:
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.
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.
That’s it. Do not hesitate to drop comment below.
Nice tutorial. Nginx is really awesome. But however it will be nice to see how Nginx is compared to Varnish Cache. Can you please post about that? Thanks
Well, honestly I don’t know that yet. I mean, I will post about Varnish as well but not its comparison to Nginx.
I do not see that index file which we created above. I still see the nginx default page. Also, the page says that we must edit the nginx.conf but you have edited the virtual.conf file does it make a difference.
Hallo bu,
Ngomong-ngomong cara setting domainnya gimana ya?
Saya saat semua setting telah selesai dan lagi ngtest install wordpress pakai IP address bisa, tapi pakai domain nggak bisa. Tolong bantu, bu. Terima kasih.
Please refer to this page:
http://servermom.wpengine.com/connecting-your-server-with-your-domain-using-free-dns-manager/512/
thanks
hello, i created as u mentioned but when i open my site(i.e. embeta.tk) the location in default.conf opened instead of the location in virtual.conf
Saya sudah membaca tutorial ibu yang berjudul “HOW TO ADD NEW SITE INTO YOUR NGINX-BASED CENTOS SERVER”.
Berarti kita bisa membuat multiple domain dalam 1 server. Apakah domain-domain tersebut menggunakan IP Publik yang sama? Sekarang saya sudah mempunyai 1 website di server dan ingin menambahkan website baru di server yang sama
Hello, I follow your guide, but in last step, sudo nginx -t
i got the warning : conflicting server name xxx.com oon 0.0.0.0:80 ignored, pls hepl me
You should double-check your nginx .conf file
Tell me how to add multiple domain in nginx?
i got problem in step 7
i got blank page when i type this command ‘nano /etc/nginx/conf.d/virtual.conf’
i follow your tutorial from stating to setup site.
plz guide me.
Use nano /etc/nginx/conf.d/default.conf
I have set up a virtual host on nginx and followed all instructions, but I can’t access my page it shows always 403 Forbidden Error.
Please suggest me what I am doing wrong.
can i see full of code in virtual.conf?
i have virtual.conf error…please help me…
I guess you missed these steps, I had to do this to enable this through firewall:
sudo firewall-cmd –permanent –zone=public –add-service=http
sudo firewall-cmd –permanent –zone=public –add-service=https
sudo firewall-cmd –reload
Also in CentOs7 I did:
sudo yum install epel-release
And
sudo systemctl restart nginx
to restart nginx.