How To Set Up Apache Virtual Hosts on Ubuntu

Virtual hosts is very useful for heavy-loaded sites to access specific directory each domain, Each domain that is configured will direct the visitor to a specific directory holding that site's information, never indicating that the same server is also responsible for other sites.

                                                                      

 
                                                
                                                            


You can get Apache installed on your server through apt-get:

sudo apt-get update
sudo apt-get install apache2
1. Create the Directory Structure

     Within each of these directories, we will create a public_html folder that will hold our actual files. This gives us some flexibility in our hosting.

sudo mkdir /var/www/demo.com/public
and test.html file under public folder and add content to test.html

2. We should also modify our permission to ensure that read access is permitted to the general web directory and all of the files and folders it contains so that pages can be served correctly:

sudo chmod -R 755 /var/www/
3. my configuration will make a virtual host for demo.com

 Then Create your <virtual-host>.conf file in the following way:

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/demo.conf
 Edit the file .conf 

sudo nano /etc/apache2/sites-available/demo.conf
add this to demo.conf 

<VirtualHost *:80>

    ServerName demo.local
    ServerAlias www.demo.local
    ServerAdmin admin@demo.local

    DocumentRoot /var/www/demo.com/public
    <Directory /var/www/demo.com/public>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>
Enable the website

sudo a2ensite demo.conf

Restart the server

sudo service apache2 restart

Then edit your /etc/hosts file in this way:

127.0.0.1   localhost demo.local www.demo.local
and Test your result

http://demo.local

Restart the server 

No comments:

Post a Comment

how to call ssh from vs code

 To call SSH from VS Code, you can use the built-in Remote Development extension. This extension allows you to open a remote folder or works...