Add new site and setup Apache virtual hosts file on CentOS server. Before you start, you better firstly know that adding and editing virtual hosts file on an Apache-based server is useful if you wish to use that server for several websites with each different domain and/or subdomain names. Any way, if it is your really first time, so let’s add the first / new domain name to your server.
- 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.
- Your server should already has necessary software to host a website. In this case are: Apache, PHP5, and MySQL.
- About 15 minutes of your time and a cup of tea if you like.
The How To Steps
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 directorty:
Step 2 – Add new document root directory for the new website. The “document root” here is a directory where you put / host all files of your website. For that, you can use this command:
mkdir -p /var/www/domain.com/public_html
mkdir -p /var/www/domain.com/htdocs
- Replace “domain.com” with your actual domain name. Do this in all part of this tutorial.
- In this example (in screenshot pics) I use “fikitips.com” as my domain. Why? Because that domain is currently unused so I use it as my testing purpose.
- You can use either the “public_html” or “htdocs” as the name of your directory but in this example I’ll use “public_html”.
Step 3 – Grant ownership permissions to the user. If you’ve switched login from user to root (step 1 above), use this command:
chown -R username:username /var/www/domain.com/public_html
If you are still logging in as user (with root privilege), use this command instead:
sudo chown -R www:www /var/www/domain.com/public_html
Step 4 – Change permissions for www directory. This is important to allow anyone (your site’s visitors) to be able to access your site. Use this simple command:
chmod 755 /var/www
it simply looks like this
Step 5 – Activate Apache virtual hosts file for the new domain. Now you have to setup and configure Apache virtual hosts file to add your new website. In this stage you need to edit “httpd.conf” file. Issue this command:
Once Nano editor screen appears, scroll down and fine following lines:
tip: You can hit Control+V to jump to the next page.
Found it? Make sure it listen to port 80.
Next, scroll down again or hit Control+V several times until you see this section (in screenshot pic) below:
Now remove / delete the # symbol. So it’ll look like this:
# NOTE: NameVirtualHost cannot be used without a port specifier
# (e.g. :80) if mod_ssl is being used, due to the nature of the
# SSL protocol.
The line means any IP address going through port 80 will be a virtual host. However if your server has more that one IPs, you can simply replace the * symbol with your IP.
Once done, right below that section you’ll see:
What you have to do:
- Remove all the # symbols before the <VirtualHost *:80> until </VirtualHost>.
- Change email address at “ServerAdmin” line.
- Change the document root path at “DocumentRoot” line.
- ErrorLog and CustomLog lines are optional but you better also set it up to log issues that arise while maintaining the server.
Shortly, it will look like this:
That’s it. Now hit Control+O on your keyboard to save followed by Control+X to exit Nano editor screen.
Step 6 – Finally restart Apache service and all its processes. Issue this command first:
apachectl -k stop
That will kill all running Apache processes. Next issue this familiar command syntax:
service httpd start
You’ll see the OK message indicating everything’s just fine and your server is ready to host a live website.
Step 7 – Give it a test. Now your server is basically ready to host your site but you better give it a test before actually deploying / moving your site in. For that purpose you can use this test page:
then add this:
<title>Apache is really working</title>
<h1>Success: You Have Set Up a Virtual Host</h1>
<p>This is test page for domain.com</p>
Save and exit.
Finally, open your most favorite web browser then access your server. You can do that by typing its IP address or domain name if you’ve updated your domain’s DNS records. It should look like this:
Congratulation, you server is now ready. You can now upload your site’s files to your server via your favorite FTP client app. Read my previous guide: how to use SFTP to upload and manage files on your server. Enjoy..
p.s: You can follow the whole steps above to add another website (with new domain name) to your server
Also do following step if you want to enable .htaccess usage for your website. Open Nano editor to edit httpd.conf file again: You’ll only need to do this once.
now find following lines:
Change “none” to “all”
Once done, hit Control+O to save followed by Control+X to exit.
Do not hesitate to ask a question related about this topic. Drop me a comment below. Do not also forget to follow me on twitter to get quicker update.