Monday, 21 September 2020

Host Multiple Websites on a Single Server with Apache on Ubuntu

Below config is to host multiple website which runs on different port on same server:-


Step1: Make two directories one for each port

mkdir /var/www/html/172.16.1.12_8080

mkdir /var/www/html/172.16.1.12_8081


Step2: Create index file for site on port 8080

nano /var/www/html/172.16.1.12_8080/index.html

<html>

<title>172.16.1.12_8080</title>

<h1>Welcome to 172.16.1.12_8080 Website</h1>

<p>Website on port 8080</p>

</html>

CTRL X then press Y

press Enter


Step3: Create index file for site on port 8081

nano /var/www/html/172.16.1.12_8081/index.html

<html>

<title>8081</title>

<h1>Welcome to 172.16.1.12_8081 Website</h1>

<p>Website on port 8081</p>

</html>

CTRL X then press Y

press Enter


Step4: Assign priviledge to sites

chown -R www-data:www-data /var/www/html/172.16.1.12_8080

chown -R www-data:www-data /var/www/html/172.16.1.12_8081


Step5: Add 8080 site 

nano /etc/apache2/sites-available/172.16.1.12_8080.conf

<VirtualHost 172.16.1.12:8080>

ServerAdmin admin@localhost

ServerName  172.16.1.12

DocumentRoot /var/www/html/172.16.1.12_8080

DirectoryIndex index.html

ErrorLog ${APACHE_LOG_DIR}/8080_error.log

CustomLog ${APACHE_LOG_DIR}/8080_access.log combined

</VirtualHost>

CTRL X then press Y

press Enter


Step6: Add 8081 site

nano /etc/apache2/sites-available/172.16.1.12_8081.conf

<VirtualHost 172.16.1.12:8081>

ServerAdmin admin@localhost

ServerName  172.16.1.12

DocumentRoot /var/www/html/172.16.1.12_8081

DirectoryIndex index.html

ErrorLog ${APACHE_LOG_DIR}/8081_error.log

CustomLog ${APACHE_LOG_DIR}/8081_access.log combined

</VirtualHost>

CTRL X then press Y

press Enter


Step7: Enable sites

a2ensite 172.16.1.12_8080

a2ensite 172.16.1.12_8081


Step7: Restart Apache

systemctl restart apache2

No comments:

Post a Comment