This tutorial will guide you how to add new site and domain to your Ubuntu server with Apache and PHP5. That includes how to add Apache virtual hosts file for the new website. Before we begin, you better firstly understand that adding and editing virtual hosts file on a server is useful if you wish to use that server for several websites with each different domain and/or subdomain names. Without this, you can still use that server to host your website but only for a single website. Anyway, if you prefer to use Nginx as your web server, you can simply read my previous article about how setup virtual hosts file on Nginx-based server so you can add new website into it.
As usual, there are some basic requirements including basic knowledge you should already fulfilled.
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 Apache virtual hosts file to add new site in Ubuntu server.
- A server running either Ubuntu or Debian. In this example I use Ubuntu 12.04.
- You’ll need either Putty in Windows or Terminal in Mac and Linux to access your server via SSH connection.
- 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.
- You have to also know all basic commands useful to SSH-ing your server. Read: Most common Unix command to manage unmanaged server.
- Make sure you have a working server with LAMP stack installed: Apache, PHP5, MySQL, and Postfix.
- 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.
Step 2 – Next, you will need to create the directory where you will and can put all the files of your website in it. Most common directory name used worldwide is either “htdocs” or “public_html”. You may define your own name as you wish but that may be confusing for future usage.
Use one of this commands:
mkdir -p /var/www/domain.com/htdocs/
mkdir -p /var/www/domain.com/public_html/
Do not forget to replace “domain.com” with your own domain name. Also, in this article, I use “htdocs” as my main root document. So if you wish to use “public_html”, then you should replace “htdocs” each time you see it here.
Step 3 – Once created, you have to also grant ownership permissions to that folder for your username. The command used for that task is:
chown -R $USER:$USER /var/www/domain.com/htdocs/
chown -R $USER:$USER /var/www/domain.com/public_html/
Step 4 – You may also need to change permission of the /var/www/ directory so your server’s users can read and write files in it. Use this command:
chmod -R 755 /var/www
Step 5 – Add an “index.html” test file to your web directory to make sure everything’s done correctly. You can put anything there or alternatively use mine, but first launch Nano editor first:
Which in my case I use:
<title>Success - Congratulation</title>
<h1>Success: You Have Set Up a Virtual Host</h1>
<p>Apache is running and setup fine</p>
Once done, hit Control+O to save then Control+X to exit.
Step 6 – It is time for the tricky part: editing Apache viartual hosts file. First thing, you can simply copy default Apache configuration using the command below:
cp /etc/apache2/sites-available/default /etc/apache2/sites-available/domain.com
next, use Nano editor to edit that config:
Step 7 – Editing the virtual hosts file. Once you are in Nano editor screen to edit “domain.com” newly created, insert a line named “ServerName” under the ServerAdmin line and type your domain name:
Optional: if you wish to access your website using “www” prefix, you can also add “ServerAlias” line under the ServerName:
Step 8 – Still editing the same file, also define the document root directory whether “htdocs” or “public_html”. In this case you need to edit “DocumentRoot” line and change it to:
Once done, simply Save and Exit (in Putty it should be Control+O then Control+X). You don’t need to edit other lines.
Step 9 – If in Nginx we have to copy that virtual hosts file from “sites-available” to “sites-neabled” folder, Apache has its own way to enable that configuration. Simply run this built-in Apache command:
Step 10 – Final step, as instructed, you have to restart or reload Apache2 service. Use this command:
service apache2 restart
or simply reload it:
service apache2 reload
Congratulation you are done. Now you can launch your favorite web browser to test it. Obviously you can do that after you’ve setup BIND9 server and update your domain NS at registrar (GoDaddy, NameCheap, etc). Alternatively, you can simply edit your local hosts file in your computer just for a test so you don’t have to update your domain NS first. Just like what I did.
Once you feel every thing is setup correctly, you can proceed to build your full website (e.g: installing WordPress, Joomla, Drupal, forum script, etc). Unlike Nginx, Apache supports .htaccess configuration fully so everything should be easier and more familiar.