How To Set Up Virtual Hosts on Ubuntu using Apache?

Linux Server Management Tips and Tricks
Post Reply
arnlweb
Site Admin
Posts: 95
Joined: Wed Jun 22, 2016 1:28 pm
Location: India
Contact:

How To Set Up Virtual Hosts on Ubuntu using Apache?

Post by arnlweb » Wed Sep 20, 2017 12:14 pm

Apache Virtual Hosts on Ubuntu.

Login as root visa SSH.

Code: Select all

sudo mkdir -p /var/www/downloadnee.com/public_html

Code: Select all

sudo mkdir -p /var/www/youtubetoany.com/public_html
Grant Permissions both directory.

Code: Select all

sudo chmod -R 755 /var/www

Code: Select all

sudo chown -R www-data:www-data /var/www
Let's create a new file in downloadnee.com.

Code: Select all

nano /var/www/downloadnee.com/public_html/index.html

Code: Select all

<html>
  <head>
    <title>Welcome to Downloadnee.com!</title>
  </head>
  <body>
    <h1>Success!  The downloadnee.com virtual host 1 is working!</h1>
  </body>
</html>
Save the file (Ctrl+X, Y, Enter)

Let's create a new file in youtubetoany.com.

Code: Select all

nano /var/www/youtubetoany.com/public_html/index.html

Code: Select all

<html>
  <head>
    <title>Welcome to Youtubetoany.com!</title>
  </head>
  <body>
    <h1>Success!  The Youtubetoany.com virtual host 2 is working!</h1>
  </body>
</html>
Save the file (Ctrl+X, Y, Enter)

Create the 1st Virtual Host File

Code: Select all

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/downloadnee.com.conf

Code: Select all

sudo nano /etc/apache2/sites-available/downloadnee.com.conf

Code: Select all

<VirtualHost *:80>
        ServerName downloadnee.com
        ServerAlias www.downloadnee.com
        ServerAdmin [email protected]
        DocumentRoot /var/www/downloadnee.com/public_html
        <Directory /var/www/downloadnee.com/public_html>
                        Options Indexes FollowSymLinks MultiViews
                        AllowOverride All
                        Order allow,deny
                        allow from all
        </Directory>    
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Create the 2nd Virtual Host File

Code: Select all

sudo cp /etc/apache2/sites-available/downloadnee.com.conf /etc/apache2/sites-available/youtubetoany.com.conf

Code: Select all

sudo nano /etc/apache2/sites-available/youtubetoany.com.conf

Code: Select all

<VirtualHost *:80>
        ServerName youtubetoany.com
        ServerAlias www.youtubetoany.com
        ServerAdmin [email protected]
        DocumentRoot /var/www/youtubetoany.com/public_html
        <Directory /var/www/youtubetoany.com/public_html>
                        Options Indexes FollowSymLinks MultiViews
                        AllowOverride All
                        Order allow,deny
                        allow from all
        </Directory>    
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Disable the default site which is defined in 000-default.conf:

Code: Select all

sudo a2dissite 000-default.conf
Enable the New Virtual Host Files

Code: Select all

sudo a2ensite downloadnee.com.conf

Code: Select all

sudo a2ensite youtubetoany.com.conf

Code: Select all

service apache2 reload
Then test both website.

http://downloadnee.com

and

http://youtubetoany.com

Post Reply