How To Add New Site Into Your Apache-based Ubuntu Server
|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.
Prerequisite
- 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/
or,..
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/
or,..
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:
nano /var/www/domain.com/public_html/index.html
Which in my case I use:
nano /var/www/servermom.com/htdocs/index.html
<html> <head> <title>Success - Congratulation</title> </head> <body> <h1>Success: You Have Set Up a Virtual Host</h1> <p>Apache is running and setup fine</p> </body> </html>
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:
nano /etc/apache2/sites-available/servermom.com
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:
ServerName domain.com
Optional: if you wish to access your website using “www” prefix, you can also add “ServerAlias” line under the ServerName:
ServerAlias www.domain.com
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:
DocumentRoot /var/www/domain.com/public_html
or,..
DocumentRoot /var/www/domain.com/htdocs
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:
a2ensite domain.com
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.
Do you have to create a new bind9 file for every domain you add to the machine? I get having to follow these steps for each domain, but what about the DNS?
Thanx a lot for this great tut! You explained everything I wanted to know in detail.
Just one question, I have to set up a webserver for my company but website will be created and loaded by an external web designer. I created a separate account (limited) for the user to use to upload the web content. I’m now struggling to create and correctly set up symlinks from the users’ /home/public_html to the /var/www root directory. Can you maybe give a tutorial on this as every time I google this I seem to get different ways of doing this but none of them has worked for me..
Thank you
I think you can simply chown the web directory to that new user and when that user is done editing the website, you can simply chown back that folder to your user or root. Or, you can simply ask them to develop locally (using XAMPP or WAMPP) then you do the migration from localhost to the server 🙂
Hi, Your tutorial is great, I followed everything and I use it to up my joomla to my local, but I have this thing, when I access my joomla in the localhost server I can access it, but when accessing in the local area network, I am getting the default Page of the Apache, the IT WORKS page, how can I access it on LAN?
Thanks in advance.
in Step 6 I have a problem:
/etc/apache2/sites-avalible/default does not exist
Update step 6 to include the .conf files:
/etc/apache2/sites-available/default.conf /etc/apache2/sites-available/domain.com.conf
From: http://askubuntu.com/a/386385
13.10 ships a newer apache configuration, where the file you are looking for is named /etc/apache2/sites-available/000-default.conf.
Note the .conf at the end, which is now required for apache to pick up on the files. So make sure the config you are creating for your site also has .conf as its extension.
hey this is great you have told everything in detail but still am not getting 🙁 everything goes smoothly till reloading apache but when i try out my domain name in browser my html file is not opening instead it shows some other website. what should i do?
Hi admin would you kindly help me fix this problem that ive got at step 6 pls
root@socialmedia:/home/rollydelcardona# cp /etc/apache2/sites-available/default /etc/apache2/sites-available/socialmedia.rollydel.org
cp: cannot stat â/etc/apache2/sites-available/defaultâ: No such file or directory
root@socialmedia:/home/rollydelcardona#
I’ve tried to follow the instruction that i saw here
http://notes.anetizer.com/www/html/notes.php
oh how to fix it, but still
No such file or directory
Thanks
try list inside /etc/apache2/sites-available/ directlry (ls) and see what files are there. Sometimes it is named as 000-default.conf or default.conf.
The best, fastest tutorial on Apache2 I have seen!!
Great words by you, really appreciate you, im also interesting and in web developing
Everything was going just as you explained it until step 6. I did step 6 and came back file does not exist. I have nothing but problems doing anything with this ubuntu server. Everything I have tried has come back with error after error. I downloaded the server for two reasons. One, to have zoneminder for security cameras. And two, to save a little money on hosing my website. So far I have spent a week, day in and day out trying to get this working, and nothing has worked. Every instruction given has proven to not work. I have even been on askubuntu.com and have almost been blocked from asking questions on what to do or what I may be doing wrong. I have followed all instructions to the letter and always end up either part way in and get errors or never get the first step going. Thinking I may just keep spending money to host my site and pay for security camera monitoring. More of a hassle than it is worth.
update I have reached step 9. Says mydomin.com does not exist! Also I have the default page up on LAN and internet with my http://www.mydomin.com but as I said i do step 9 and says it doesn’t exist. And evertime I go back through the steps I have to redo the vh step and save. It is like I can’t change it. Help!!!!!!!!
I have followed all the steps and when i get to step 9, i put in the command and get a error “ERROR: Site domain.com does not exist!
Where hav i gone wrong, please help