In this article, I will be telling you that how can you add new domain and site to your Ubuntu server with PHP5 and Apache. Adding Apache virtual hosts file for the new website is also included in it. If you are going to use this server for multiple websites with each different subdomain and/or domain names, then it is better if you understand this that editing and adding virtual hosts file on a server is useful if you are going to use this server for this purpose. If you don’t do this, then you can only use this server for just one website.
- You need a server which is running on either Debian or Ubuntu. I am using Ubuntu 12.04 in this article.
- To access your server via SSH connection, you need either Terminal in Linux and Mac, or Putty in Windows.
- I am using Putty in this article.
- You should also know about all the basic and common commands which are used for SSH in the server.
- You should have a working server, and LAMP stack should be installed in it. (PHP5, Apache, MySQL and Postfix).
- May be now or later after you add website into your server, you can also set up Bind9 DNS Software.
The first step is that you have to login to your server through SSH connection. It must be in port 22 if you didn’t change it, or else you can also use your own defined port. Now you have to login by your username, not root. Now you have to type su once you are logged in, now press enter and then type your root password.
Now to put all the files of your website, you have to create a directory. The names which are most commonly used for this directory worldwide are “public_html” and “htdocs”. But you can use any name that you want. Enter one of the following command:
You have to write your own domain name where ever you see domain.com. And as my main root document I am using htdocs. So you can replace it each time with public_html if you are using this as a name.
Now you have to make sure that everything that you have done is fine and correct. You can do this by adding a test file to your web directory. The test file will be “index.html”. you can put anything in there. but right now, you have to launch the nano editor.
After finishing, you have to save it by pressing Control+O and then exit by pressing Control+X. now you will see the following screen in front of you:
Now the tricky part is here. Now you are going to edit the virtual hosts file of Apache. By using the command below, you can simply copy default Apache configuration:
cp /etc/apache2/sites-available/default /etc/apache2/sites-available/domain.com
now you have to use the nano editor to edit the configuration:
Now in this step, I will be telling you how to edit the virtual hosts file. Now when you are in the nano editor you have to edit the “domain.com”, you have to insert a line that should be named as “ServerName”, beneath ServerAdmin line, and there you have to type your domain name:
Now this step is optional, because if you want your website to be accessed by the prefix “www”, then another line should be added below the ServerName line. And that line would be “ServerAlias”:
You have to define the document root directory whether public_html or htdocs, while still editing the same file. But the DocumentRoot line should be edited in this scenario, and then change it to the following:
When you are finished with this, then you simply have to save and exit. Other lines don’t need to be edited.
Now the final step is that you have to reload or restart the Apache2 service. to do this, you have to enter the following command:
service apache2 restart
or if you want to simply reload it, then enter this command:
service apache2 reload
Now you are done. Congratulations. Now you can test it by launching your favorite web browser. But you can only do that if you have already set up the BIND9 server. And update your domain NS at registrar (NameCheap, GoDaddy, etc). otherwise you can also simply edit the local hosts file in your computer, to test it, so that you don’t have to update your domain NS first.
After you think that everything is working perfectly, then you can go forward to build your full website. Apache supports .htaccess configuration fully, unlike Nginx, so everything should be more familiar and easier.
So this was it for today. Now you know everything there is to add a new site to your Apache based Ubuntu server. I hope this article helped you a lot and you enjoyed it.
Hope to see you soon. Thanks. !!