Learn how to setup virtual web hosting for multiple domains to be server from one single server.
How to setup virtual web hosting and host multiple sites on one server ? [ In two minute ] : Intelligent computing
Hey guys, welcome back to Intelligent computing, previously i posted HOW TO SETUP YOUR AWS AND LAMP TO RUN YOUR FIRST PHP APP? Followed by HOW TO SETUP AND CONFIGURE LAMP ON UBUNTU SERVER? And its great piece of article to setup aws and deploy php web app/site on root of apache.
But if you are having a pretty robust server and/or you planning to deploy multiple websites on single server under multiple domains, you need to step ahead and create virtual hosts in apache2 settings, which i’ll explain how you can proceed and get your new site up and running on same server in few minutes.
If you are running all application in codeigniter, you can setup all application in single codeigniter installation[ Read How i use Codeigniter to Manage Multiple Versions of app and switch in seconds?]
This procedure i am explaining is for ubuntu with apache2 so everything going to be accordingly.
Web Root                 :           /var/www/html/
Apache2                   :           /etc/apache2/
Site configurations            :           /etc/apache2/sites-available/

Setting up sites in directory

First of all you need to make sure your website/web app resides somewhere in web root directory, suppose you’ve directory called sites
/var/www/html/sites/
And you have 2 sites in it with folder site1.com and site2.com
/var/www/html/sites/site1.com/
/var/www/html/sites/site2.com/

Pointing your domain to server

You can simply put IP address of server in your DNS setting which will start pointing your domain to server where you are about to host your websites/web app.
It might take 30 minute to 1 hour to complete this process, you can check this as follow
This will show your server’s IP address once it’s updated.
Now let’s proceed to virtual host creation.

Create virtual host configuration

  1. Copy /etc/apache2/sites-available/000-default.conf to /etc/apache2/sites-available/site1.com.conf
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/site1.com.conf
  1. Open /etc/apache2/sites-available/site1.com.conf with your favourite editor in full admin/sudo mode and edit.
sudo vi /etc/apache2/sites-available/site1.com.conf
  1. Edit file and change following lines and replace with yours configuration
ServerAdmin youremail@hostname.com
DocumentRoot /var/www/html/sites/site1.com/
ErrorLog ${APACHE_LOG_DIR}/site1_error.log
CustomLog ${APACHE_LOG_DIR}/site1_access.log combined
These 4 lines are clear
     Your email address for webmaster
     Document root for your website folder
     Error log name to identify error of your site
     Access log file name to identify log for your site
You are almost done.

Enable site configuration

Now you need to enable this configuration to enable site on your machine.
For this you need to execute simple a2ensite command for your newly configured site.
sudo a2ensite site1.com.conf
And yes you have enabled this site in your configuration, you are one command away to make your site1.com live, just restart your apache and done.
sudo service apache2 restart
Once the command is executed successfully, your site site1.com can be accessed from web directly and you can see web pages/apps stores in /var/www/html/sites/site1.com/directory

What’s next?

If you face any problem in saving or execution of apache, check everything once again, for any typo or similar stuff.
Also do not forget to check apache error log if any, and if you are not able to restart apache.
If you find it useful, please share it to your peers on social media and follow @inteligentcomp on twitter/facebook for more updates.
Got any burning question? Write us at admin@inteligentcomp.com

http://Inteligentcomp.com