{{announcement.body}}
{{announcement.title}}

How to Install and Configure Apache2

DZone 's Guide to

How to Install and Configure Apache2

In this article, we will see how to install and configure Apache2 web server in Ubuntu 16.04.

· Integration Zone ·
Free Resource

In this article, we will see how to install and configure Apache2 web server in Ubuntu 16.04.

Note: Throughout this article, we will be referring to domain name as website1-example.com. Replace this domain name with your actual domain name whenever required.

Step 1: Install Apache2 Web Server

We will begin with updating the local package to reflect the latest upstream changes. Afterwards we can install the Apache2 package.

Java
 




x


 
1
$ sudo apt-get update 
2
$ sudo apt-get install apache2



The status can be check by running the following commands

Java
 




xxxxxxxxxx
1


 
1
$ sudo systemctl status apache2



You can access the default apache landing page to confirm that the software is running properly. You can access this through your server’s domain name or IP address.

Step 2: Check Web Server

Run below command to make sure the service running

Java
 




xxxxxxxxxx
1


 
1
$ sudo systemctl status  apache2



Now you can access the default apache landing page to confirm that the software is running properly. You can access it through your server’s domain name or IP address.

For example: http://www.website1-example.com

Step 3: Create Virtual Host

In Apache on Ubuntu, all the virtual host configuration files are stored under /etc/apache2/sites-available directory. With the new Apache installation, you can find a default virtual host file called 000-default.conf there. We will create a new virtual host configuration file by copying 000-default.conf file.

Java
 




xxxxxxxxxx
1


 
1
$ sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/website1-example.com.conf



Open your virtual host file:

Java
 




xxxxxxxxxx
1


 
1
$ sudo nano /etc/apache2/sites-available/website1-example.com.conf



The file should look like the following:

Java
 




xxxxxxxxxx
1


 
1
<VirtualHost *:80> 
2
ServerAdmin webmaster@localhost     
3
      DocumentRoot /var/www/html     
4
      ErrorLog ${APACHE_LOG_DIR}/error.log     
5
      CustomLog ${APACHE_LOG_DIR}/access.log combined 
6
</VirtualHost>



Now edit this file as per your requirement. My configuration looks like below:

Java
 




xxxxxxxxxx
1
14


 
1
<VirtualHost *:8090> 
2
ServerAdmin webmaster@website1-example.com 
3
ServerName website1-example.com 
4
ServerAlias www.website1-example.com 
5
DocumentRoot /var/www/website1-example.com 
6
<Directory /var/www/website1-example.com> 
7
Options Indexes FollowSymLinks MultiViews    
8
AllowOverride All    
9
Require all granted    
10
allow from all 
11
</Directory> 
12
ErrorLog /var/www/website1-example.com/error.log 
13
CustomLog /var/www/website1-example.com/access.log combined 
14
</VirtualHost>



  • ServerAdmin: Server admin’s email address.
  • ServerName: The domain that should match for this virtual host configuration. This should be your domain name. i.e. website1-example.com
  • ServerAlias: It is an additional matching condition that needs to be processed. i.e. http://www.website1-example.com
  • DocumentRoot: The directory from which Apache will serve the domain files.
  • Options: This directive controls which server features are available in a specific directory.
  • ErrorLog, CustomLog: Specifies the location of log files.

Step 4: Create Project Directory

By default, the document root directory is /var/www/html. We will create a website1-example.com directory in www directory as defined in the above virtual host configuration. 

Java
 




xxxxxxxxxx
1


 
1
$ sudo mkdir /var/www/website1-example.com



Now let’s create a test HTML file called index.html in a root directory we just created in a previous step. 

Java
 




xxxxxxxxxx
1


 
1
$ sudo nano /var/www/website1-example.com/index.html



Add the following code to the file and then save it.

HTML
 




xxxxxxxxxx
1


 
1
<html> 
2
<head> 
3
<title>website1-example.com</title> 
4
</head> 
5
<body>  
6
 <h2> Welcome to website1-example.com </h2> 
7
</body> 
8
</html>



Step 5: Enable the Virtual Host

Enable the virtual host using the a2ensite tool: 

Java
 




xxxxxxxxxx
1


 
1
$ sudo a2ensite website1-example.com.conf



Apply the changes to Apache

Java
 




xxxxxxxxxx
1


 
1
$ sudo service apache2 reload



Next, open /etc/hosts file in editor and add your domain/IP address like below:

Java
 




xxxxxxxxxx
1


 
1
$ sudo nano /etc/hosts 
2
[...] 
3
127.0.0.1 localhost 
4
your-domain your-sever-name.com



For example:

Java
 




xxxxxxxxxx
1


 
1
13.233.10.119 website1-example.com 
2
[...]


Save and close the file.

Step 6: Enable CORS

Now we will enable CORS on Apache2 server. CORS is a process which tells browsers to access resources from different origin (domain, protocol, port) via HTTP headers 

Enable headers by typing:

Java
 




xxxxxxxxxx
1


 
1
$ sudo a2enmod headers



Open /etc/apache2/apache2.conf file by typing following command and add cross-origin headers in <Directory> section

Java
 




xxxxxxxxxx
1


 
1
$ sudo nano /etc/apache2/apache2.conf



For example:

Figure: CORS Configuration

Step 7: Enable Ports

If you are using ports other than default port number 80 then we need to enable that port. In step 3 we have configured a virtual host on port 8090. Let’s enable port 8090 in Apache2.

Open /etc/Apache2/ports.conf file. In this file add your port number. 

For example:

Java
 




xxxxxxxxxx
1


 
1
$ sudo nano /etc/apache2/ports.conf 
2
 [...] 
3
Listen 80 
4
Listen 8090 
5
[...]



Save and close the file.

Restart your apache2 service to reflect all changes.

Java
 




xxxxxxxxxx
1


 
1
$ sudo service apache2 restart



Thanks for reading!

Topics:
apache 2 ,domain ,html ,http headers ,ip address ,server architecture ,server client ,ubuntu ,virtual host ,web server

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}