# - - # # - Run multiple websites on Ubuntu Server - Apache2 - # https://www.liberiangeek.net/2014/09/run-multiple-websites-single-ubuntu-server-using-apache2/ # - More documentation - # https://www.digitalocean.com/community/tutorials/how-to-set-up-apache-virtual-hosts-on-ubuntu-14-04-lts https://www.liberiangeek.net/2014/09/run-multiple-websites-single-ubuntu-server-using-apache2/ https://www.digitalocean.com/community/tutorials/how-to-set-up-a-host-name-with-digitalocean Apache2 is the most popular web server on the Internet right now. It’s way ahead of other open source web servers and it’s highly supported and extremely flexible. Apache2 strength is that it’s broken into multiple components with specific functions. Each component has a role to play and this makes thing a bit easier to manage. One of the many components of Apache2 is the use of virtual host. Virtual hosting via Apache2 allows for a single server with single IP address to host multiple Apache2 web content. For instance, if you have one big server with more than enough resources, you can host multiple websites on that single machine. www.myblog.com, www.mynewblog.com, www.tecblog.com can all be hosted on the same server without purchasing additional servers. This is a great and fantastic way to host your many blogs using Apache2. This blog post is going to show you how to make that happen in Ubuntu. To make this happen you need to create separate directories to host each website content. For this blog post, we’re going to be hosting www.myblog.com, www.mynewblog.com and www.techblog.com. Installing Apache2 in Ubuntu First, lets install Apache2 in Ubuntu. To do that, run the commands below. sudo apt-get install apache2 After installing Apache2, go and create three separate directory for each website. To do that, run the commands below sudo mkdir -p /var/www/myblog.com/html/ sudo mkdir -p /var/www/mynewblog.com/html/ sudo mkdir -p /var/www/techblog.com/html/ md -p /home/Data1/WebServer/www/website/myblog.com/html/ md -p /home/Data1/WebServer/www/website/mynewblog.com/html/ md -p /home/Data1/WebServer/www/website/techblog.com/html/ mkdir -p /home/Data-1/WebServer/www/website/accounting.com/html/ mkdir -p /home/Data-1/WebServer/www/website/inventory.com/html/ mkdir -p /home/Data-1/WebServer/www/website/mysales.com/html/ After creating those three directories, the next step is to grant permissions to Apache2 for these directories. To do that, run the commands below. sudo chown -R apache:apache /var/www/ Then give Apache the correct permissions to manage files and folder in these directories. sudo chmod -R 755 /var/www/ Now that the separate directories are created, how would you tell each one from the other. To do that, we’ll going to be creating separate and unique pages for each of the site. Our basic sample pages will carry a simple line of text with the name of the website. To do that, create a simple index.html page for www.myblog.com vim /home/Data1/WebServer/www/website/myblog.com/html/index.html
It work! Welcome to myblog.com
vim /home/Data1/WebServer/www/website/mynewblog.com/html/index.htmlIt work! Welcome to mynewblog.com
vim /home/Data1/WebServer/www/website/techblog.com/html/index.htmlIt work! Welcome to techblog.com
vim /home/Data-1/WebServer/www/website/myaccounting.com/html/index.htmlIt work! Welcome to myaccounting.com
vim /home/Data-1/WebServer/www/website/myinventory.com/html/index.htmlIt work! Welcome to myinventory.com
vim /home/Data-1/WebServer/www/website/mysales.com/html/index.htmlIt work! Welcome to mysales.com
After that go and create virtual host directives for each of the three websites. To do that, go and copy the default Apache site configuration file and create our virtual hosts from it. Run the commands below to do it. sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/myblog.com.conf sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/mynewblog.com.conf sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/techblog.com.conf sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/myaccounting.com.conf sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/myinventory.com.conf sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/mysales.com.conf Now that the three separate virtual hosts are created, sudo vim /etc/apache2/sites-available/myblog.com.conf sudo vim /etc/apache2/sites-available/myaccounting.com.conf sudo vim /etc/apache2/sites-available/myinventory.com.conf sudo vim /etc/apache2/sites-available/mysales.com.conf # - To test - # ServerAdmin admin@myblog.com ServerAdmin webmaster@myblog.com ServerName myblog.com ServerAlias www.myblog.com DocumentRoot /var/www/myblog.com/html ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined ServerAdmin admin@myaccounting.com ServerName myaccounting.com ServerAlias www.myaccounting.com DocumentRoot /home/Data-1/WebServer/www/website/myaccounting.com/html ServerAdmin admin@myinventory.com ServerName myinventory.com ServerAlias www.myinventory.com DocumentRoot /home/Data-1/WebServer/www/website/myinventory.com/html ServerAdmin admin@mysales.com ServerName mysales.com ServerAlias www.mysales.com DocumentRoot /home/Data-1/WebServer/www/website/mysales.com/html Do the same for each of the other websites and replace the highlighted lines to correspond with the website. The next things to do is enable the sites. To do that, run the commands below sudo a2ensite myblog.com.conf sudo a2ensite mynewblog.com.conf sudo a2ensite techblog.com.conf sudo a2ensite accounting.com.conf sudo a2ensite inventory.com.conf sudo a2ensite mysales.com.conf # - - # # - deactivate website - # sudo a2dissite sales.com.conf systemctl reload apache2 Restart Apache and test your sites: sudo service apache2 restart Now, open your local host file from any machine and enter the same server IP address for each of the website, then save and open your browser and type the domain name. sudo vim /etc/hosts 127.0.0.1 localhost 192.168.6.131 myblog.com 192.168.6.131 mynewblog.com 192.168.6.131 techblog.com uServerHome 127.0.0.1 localhost 127.0.1.1 uServerHome 192.168.1.100 myaccountin.com 192.168.1.100 myinventory.com 192.168.1.100 mysales.com # - - # # - Very important note - # Note: In case, you want to access the above sites from a remote system, you need to add the above two lines to your remote system’s /etc/hosts/ file as well. Don’t forget to replace the IP address with your own. # - - # # - Do this in your own computer as well - # sudo vim /etc/hosts 127.0.0.1 localhost 192.168.1.200 myblog.com 192.168.1.200 mynewblog.com 192.168.1.200 techblog.com # - - # # - Do this in your own computer as well - # # - HPDesktopHome - # sudo gedit /etc/hosts 127.0.0.1 localhost 127.0.1.1 uServerHome 127.0.0.1 localhost 127.0.1.1 HPDLinuxMint181 # - Local - # 192.168.1.100 myaccounting.com 192.168.1.100 myinventory.com 192.168.1.100 mysales.com # - From isdevelopment.us - # isdevelopment.us myaccounting.com isdevelopment.us myinventory.com isdevelopment.us mysales.com # - From the real IP address - # 24.46.78.201 myaccounting.com 24.46.78.201 myinventory.com 24.46.78.201 mysales.com 24.46.78.201 www.myaccounting.com 24.46.78.201 www.myinventory.com 24.46.78.201 www.mysales.com # The following lines are desirable for IPv6 capable hosts ::1 ip6-localhost ip6-loopback fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters # - - # # - Configuring hosts file in Windows - # https://support.rackspace.com/how-to/modify-your-hosts-file/ Modify your hosts file 5-6 minutes Modifying your hosts file enables you to override the DNS for a domain, on that particular machine. This is useful when you want to test your site without the test link, prior to going live with SSL; verify that an alias site works, prior to DNS changes; and for other DNS-related reasons. Modifying your hosts file causes your local machine to look directly at the IP address specified. To modify the hosts file, you add two entries to the file that contains the IP address that you want the site to resolve to and the address. Adding the following two lines, for example, point www.domain.com and domain.com to our current PHP5-ITK (“Refreshed” PHP5) cluster: 64.49.219.194 www.domain.com 64.49.219.194 domain.com The sections in this article provide instructions for locating and editing the hosts file on the following operating systems: Windows 10, Windows 8, Windows 7, and Windows Vista Windows NT, Windows 2000, and Windows XP Linux Mac OS X 10.0 through 10.1.5 Mac OS X 10.6 through 10.12 After you add the domain information and save the file, your system begins resolving to the specified IP address. After testing is finished, remove these entries. Windows Windows 10, Windows 8, Windows 7, and Windows Vista use User Account Control (UAC), so Notepad must be run as Administrator. For Windows 10 and 8 Press the Windows key. Type Notepad in the search field. In the search results, right-click Notepad and select Run as administrator. From Notepad, open the following file: c:\Windows\System32\Drivers\etc\hosts Make the necessary changes to the file. Click File > Save to save your changes. For Windows 7 and Vista Click Start > All Programs > Accessories. Right-click Notepad and select Run as administrator. Click Continue on the Windows needs your permission UAC window. When Notepad opens, click File > Open. In the File name field, type C:\Windows\System32\Drivers\etc\hosts. Click Open. Make the necessary changes to the file. Click File > Save to save your changes. Windows NT, Windows 2000, and Windows XP Click Start > All Programs > Accessories > Notepad. Click File > Open. In the File name field, type C:\Windows\System32\Drivers\etc\hosts. Click Open. Make the necessary changes to the file. Click File > Save to save your changes. Linux Open a terminal window. Open the hosts file in a text editor (you can use any text editor) by typing the following line: sudo nano /etc/hosts Enter your domain user password. Make the necessary changes to the file. Press Control-x. When asked if you want to save your changes, answer y. Mac OS X 10.0 through 10.12 Mac OS X 10.0 through 10.1.5 Open /Applications/Utilities/NetInfo Manager. To allow editing of the NetInfo database, click the padlock in the lower-left corner of the window. Enter your domain user password and click OK. In the second column of the browser view, select the node named machines. The third column contains entries for -DHCP-, broadcasthost, and localhost. In the third column, select localhost. From the Edit menu, select Duplicate. (The quickest way to create a new entry is to duplicate an existing one.) A confirmation alert appears. Click Duplicate. A new entry called localhost copy appears, and its properties are shown below the browser view. Double-click the value of the ip_address property and enter the IP address of the other computer. Double-click the value of the name property and enter the hostname you want for the other computer. Click the serves property and select Delete from the Edit menu. From the File menu, select Save. A confirmation alert appears. Click Update this copy. Repeat steps 6 through 12 for each additional host entry that you want to add. From the NetInfo Manager menu, select Quit. You do not need to restart the computer. Mac OS X 10.6 through 10.12 Open Applications > Utilities > Terminal. Open the hosts file by typing the following line in the terminal window: sudo nano /private/etc/hosts Type your domain user password when prompted. Edit the hosts file. The file contains some comments (lines starting with the # symbol), and some default hostname mappings (for example, 127.0.0.1 – local host). Add your new mappings after the default mappings. Save the hosts file by pressing Control+x and answering y. Make your changes take effect by flushing the DNS cache with the following command: dscacheutil -flushcache The new mappings should now take effect. Start building on our Managed Cloud today. Sign Up Now