How To Add Multi Websites in Ubuntu 20.04

Here is the very simple method how to host multi websites on ubuntu

 

  1. Open SSH or FTP using root login
  2. Type: cd /var/www/
  3. Add your first domain Use This Command: Sudo mkdir -p /var/www/example1.com/public_html
  4. to add more websites you can add your other domain in the same way. Command mkdir -p /var/www/example2.com/public_html
  5. Give Full Permission to these Website directory Command: sudo chown -R www-data: /var/www/example1.com
  6. do same with other domains Command: sudo chown -R www-data: /var/www/dexample2.com

 

Creating Virtual Hosts On Ubuntu systems, Apache Virtual Hosts configuration files are located in 

/etc/apache2/sites-available directory.

They can be enabled by creating symbolic links to the 

/etc/apache2/sites-enabled directory,

which Apache read during the startup.

Open your text editor of choice and create the following basic Virtual Host configuration file:

/etc/apache2/sites-available/example.com.conf
<VirtualHost *:80>
    ServerName domain1.com
    ServerAlias www.domain1.com
    ServerAdmin [email protected]
    DocumentRoot /var/www/domain1.com/public_html

    <Directory /var/www/domain1.com/public_html>
        Options -Indexes +FollowSymLinks
        AllowOverride All
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/domain1.com-error.log
    CustomLog ${APACHE_LOG_DIR}/domain1.com-access.log combined
</VirtualHost>

  • ServerName: The domain that should match for this virtual host configuration. This should be your domain name.
  • ServerAlias: All other domains or subdomains that should match for this virtual host such as the www subdomain.
  • DocumentRoot: The directory from which Apache will serve the domain files.

 

To enable the new virtual host file, use the a2ensite helper script which creates a symbolic link from the virtual host file to the sites-enabled directory:

sudo a2ensite domain1.com


The other option is to manually create a symlink as shown below:
sudo ln -s /etc/apache2/sites-available/domain1.com.conf /etc/apache2/sites-enabled/

Once done, test the configuration for any syntax errors with:

sudo apachectl configtest

If there are no errors, you will see the following output:

Syntax OK

Restart the Apache service for the changes to take effect:

sudo systemctl restart apache2

Finally, to verify that everything is working as expected, open http://example1.com in your browser. 

  • 28 användare blev hjälpta av detta svar
Hjälpte svaret dig?

Relaterade artiklar

Install Let's Encrypt on Ubuntu 20.04

Step 1 — Installing Certbot sudo apt install certbot python3-certbot-apache Obtaining an...

404 Not Found The requested URL was not found on this server WordPress and Ubuntu

Step 1. Edit in apache2.conf  cd /etc/apache2 sudo gedit apache2.conf Step 2. Here in...