25 C
Texas
Melhttp://www.osradar.com
Guru Unix /Linux and GNU supporter

How to Create Apache Virtual Hosts on Ubuntu 20.04 /Debian 10.4

Maybe you want to run more then1 domain on the same Server, why buying another server while its possible to run many sites on the same machine.

Let me show you in this small tutorials how to create Apache Virtual hosts on your Ubuntu 20.04 Server or Debian 10.4 .

Let me give an idea about my server :

Server = Debian 10.4

- Advertisement -

IP = 192.168.0.100

1-Install Apache

Update your server

#apt-get update 
#apt-get upgrade 

So, Install Apache on Debian or Ubuntu just with this command :

root@osradar-debian:~# apt  install  apache2
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  apache2-bin apache2-data apache2-utils ca-certificates libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap libbrotli1 libcurl4 libgdbm-compat4 libgdbm6 libjansson4 libldap-2.4-2 libldap-common liblua5.2-0 libnghttp2-14
  libperl5.28 libpsl5 librtmp1 libsasl2-2 libsasl2-modules libsasl2-modules-db libssh2-1 openssl perl perl-modules-5.28 publicsuffix ssl-cert
Suggested packages:
  apache2-doc apache2-suexec-pristine | apache2-suexec-custom www-browser libsasl2-modules-gssapi-mit | libsasl2-modules-gssapi-heimdal libsasl2-modules-ldap libsasl2-modules-otp libsasl2-modules-sql perl-doc libterm-readline-gnu-perl
  | libterm-readline-perl-perl make libb-debug-perl liblocale-codes-perl openssl-blacklist
The following NEW packages will be installed:
  apache2 apache2-bin apache2-data apache2-utils ca-certificates libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap libbrotli1 libcurl4 libgdbm-compat4 libgdbm6 libjansson4 libldap-2.4-2 libldap-common liblua5.2-0
  libnghttp2-14 libperl5.28 libpsl5 librtmp1 libsasl2-2 libsasl2-modules libsasl2-modules-db libssh2-1 openssl perl perl-modules-5.28 publicsuffix ssl-cert
0 upgraded, 30 newly installed, 0 to remove and 0 not upgraded.
Need to get 12.1 MB of archives.
After this operation, 60.8 MB of additional disk space will be used.
Do you want to continue? [Y/n]

Now, you have to enable and start Apache. To do it, run the following commands.

root@osradar-debian:~# systemctl enable apache2
Synchronizing state of apache2.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable apache2
root@osradar-debian:~# systemctl start apache2

So, Open your web browser and go to http://localhost or http://your-server

This is the standard home page of Apache. So, Apache is installed..

Now the change is we have to configure to sites on the same servers

www.fbi.gov
www.cia.gov 

So, Let us create and define the folders to the new virtualhosts:

root@osradar-debian:~# mkdir /var/www/html/fbi
root@osradar-debian:~# mkdir /var/www/html/cia

Then, create a new index.html file for the FBI site:

vi /var/www/html/fbi/index.html

And add the following:

<html>
 <head>
 <title>www.fbi.gov</title>
 </head>
 <body>
 <h1>Hello, Welcome to www.fbi.com  website</h1>
 </body>
</html>

Now, do the same with the the CIA site:

vi /var/www/html/cia/index.html

Again, add this content.

<html>
 <head>
 <title>www.cia.gov</title>
 </head>
 <body>
 <h1>Hello, Welcome to www.cia.com  website</h1>
 </body>
</html>

Create site configurations

The next step is create the new configurations file to the new Virtualhosts:

vi /etc/apache2/sites-available/cia.conf

So, add the following:

<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName cia.gov
    ServerAlias www.cia.gov
    DocumentRoot /var/www/html/cia

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

</VirtualHost>

Now, it is the FBI’s turn:

vi /etc/apache2/sites-available/fbi.conf

Add the following:

<VirtualHost *:80>

    ServerAdmin [email protected]
    ServerName fbi.gov
    ServerAlias www.fbi.gov
    DocumentRoot /var/www/html/fbi

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

</VirtualHost>

Save the changes and close the file.

To enable newly created virtual hosts run the following commands as the root user:

a2ensite fbi
a2ensite cia

Restart or Reload Apache

systemctl reload apache2

Lets make an test from another server / machine

add this to the hosts file

From linux

add to the /etc/hosts file

192.168.0.100 cia.gov fbi.gov

For windows add the same to c:\windows\system32\drivers\etc\hosts

Are y ready ?

For LEMP full install please visit How to install LEMP on Ubuntu 20.04

- Advertisement -
Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"

LEAVE A REPLY

Please enter your comment!
Please enter your name here



Latest article