How to create multiple host websites on an ubuntu 18.04
Multiple hosts create its main use Ubuntu 18.04 and Apache is the ability to host multiple websites on a single server. Here client request base Apache virtual host directs visitors to different folders where the domains file are located.
Apache don’t have installed, you can run the below command on Ubuntu 18.04 server.
$ sudo apt-get install apache2
Step 1: Create file
We will need to create a directory structure that will host our website’s data. Apache directory is under the /var/www path. here create sub-directory for our two domains.
$ sudo mkdir -p /var/www/admin.localhost/public_html
$ sudo mkdir -p /var/www/backups.localhost/public_html
Step 2: Directory ownership Change
Change Directory ownership main use allow the current logged user to modify files.
$ sudo chown -R $USER:$USER /var/www/admin.localhost/public_html
$ sudo chown -R $USER:$USER /var/www/backups.localhost/public_html
Step 3: Modify the file permissions
Modify the file permissions is to use grant read access to the two directories. and the web pages publicly accessible.
$ sudo chmod -R 755 /var/www
Step 4: Create virtual host/domain
We will create a sample index.html file for admin.localhost.
$ sudo vi /var/www/admin.localhost/public_html/index.html
#admin.localhost/public_html/index.html
<html>
<body>
This is our admin.localhost website
</body>
</html>
We will create a sample index.html file for backups.localhost.
$ sudo vi /var/www/backups.localhost/public_html/index.html
#backups.localhost/public_html/index.html
<html>
<body>
This is our backups.localhost website
</body>
</html>
Step 5: Create the virtual hosts configuration files
Default virtual host file location path is /etc/apache2/sites-available/000-default.conf. simple we need to copy that file and admin.localhost and backups.localhost virtual hosts.
$ sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/admin.conf
$ sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/backups.conf
Now, copy file and open admin.conf file:
$ sudo vi /etc/apache2/sites-available/admin.conf
#/etc/apache2/sites-available/admin.conf
<VirtualHost *:80>
ServerName admin.localhost
ServerAlias www.admin.localhost
ServerAdmin webmaster@localhost
DocumentRoot /var/www/admin.localhost/public_html
<Directory /var/www/admin.localhost/public_html/>
Options Indexes FollowSymLinks MultiViews
Options All
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
And the same procedure backups.localhost.
$ sudo vi /etc/apache2/sites-available/backups.conf
#/etc/apache2/sites-available/backups.conf
<VirtualHost *:80>
ServerName admin.localhost
ServerAlias www.admin.localhost
ServerAdmin webmaster@localhost
DocumentRoot /var/www/backup.localhost/public_html
<Directory /var/www/backup.localhost/public_html/>
Options Indexes FollowSymLinks MultiViews
Options All
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Step 6: Virtual hosts enable
Now create two configuration file is enable them using command line :
$ sudo a2ensite admin.conf
$ sudo a2ensite backups.conf
Now run the below command :
$ systemctl reload apache2
Step 7: Restart Apache
$ sudo service apache2 restart
Step 8: Edit the local hosts file
You are running linux, edit the /etc/hosts file using the command below:
$ sudo vi /etc/hosts
Add the below code and save.
127.0.0.1 backups.localhost www.backups.localhost
127.0.0.1 admin.localhost www.admin.localhost
Step 9: Test your virtual hosts
Now run your browser in admin.localhost and backups.localhost.
Url : admin.localhost
Url : backups.localhost
WHAT COMMAND CREATES A SUBDIRECTORY UNDER A DIRECTORY.WHAT COMMAND CREATES A SUBDIRECTORY UNDER A DIRECTORY?