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

0
51

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.

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. Your server should already has necessary software to host a website. In this case are: Apache, PHP5, and MySQL.
  5. 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.

switch-root-login

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

1
cd ~

Step 2Add 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:

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

or,..

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

some notes:

  • 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 3Grant ownership permissions to the user. If you’ve switched login from user to root (step 1 above), use this command:

1
chown -R username:username /var/www/domain.com/public_html

example:

chown command

If you are still logging in as user (with root privilege), use this command instead:

1
sudo chown -R www:www /var/www/domain.com/public_html

Step 4Change 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:

1
chmod 755 /var/www

it simply looks like this

chmod 755

Step 5Activate 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:

1
nano /etc/httpd/conf/httpd.conf

Once Nano editor screen appears, scroll down and fine following lines:

1
2
#Listen 12.34.56.78:80
Listen 80

tip: You can hit Control+V to jump to the next page.

Found it? Make sure it listen to port 80.

listen port 80

Next, scroll down again or hit Control+V several times until you see this section (in screenshot pic) below:

name virtual hosts

Now remove / delete the # symbol. So it’ll look like this:

1
2
3
4
5
6
NameVirtualHost *:80
#
# 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:

virtual hosts section

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:

1
2
3
4
5
6
7
8
<VirtualHost *:80>
     ServerAdmin youremail@domain.com
     DocumentRoot /var/www/domain.com/public_html
     ServerName www.domain.com
     ServerAlias domain.com
     ErrorLog /var/www/domain.com/error.log
     CustomLog /var/www/domain.com/requests.log
</VirtualHost>

An example:

example virtual host

That’s it. Now hit Control+O on your keyboard to save followed by Control+X to exit Nano editor screen.

Step 6Finally restart Apache service and all its processes. Issue this command first:

1
apachectl -k stop

That will kill all running Apache processes. Next issue this familiar command syntax:

1
service httpd start

or,..

1
/etc/init.d/httpd start

You’ll see the OK message indicating everything’s just fine and your server is ready to host a live website.

start apache again

Step 7Give 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:

1
/var/www/domain.com/public_html/index.html

then add this:

1
2
3
4
5
6
7
8
9
<html>
  <head>
    <title>Apache is really working</title>
  </head>
  <body>
    <h1>Success: You Have Set Up a Virtual Host</h1>
    <p>This is test page for domain.com</p>
  </body>
</html>

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:

success

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

Another tip:

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.

1
nano /etc/httpd/conf/httpd.conf

now find following lines:

1
2
3
4
<directory />
    Options FollowSymLinks
    AllowOverride none
</directory>

Change “none” to “all”

<directory />
    Options FollowSymLinks
    AllowOverride all
</directory>

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

enable htaccess

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.



LEAVE A REPLY

Please enter your comment!
Please enter your name here