DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • How Java Servlets Work: The Backbone of Java Web Apps
  • Multiplatform Directory Bookmarks on the Command Line
  • Java Z Garbage Collector (ZGC): Revolutionizing Memory Management
  • How To Build a Google Photos Clone - Part 1

Trending

  • Understanding and Mitigating IP Spoofing Attacks
  • Performance Optimization Techniques for Snowflake on AWS
  • Build an MCP Server Using Go to Connect AI Agents With Databases
  • Accelerating AI Inference With TensorRT
  1. DZone
  2. Coding
  3. Java
  4. How to Install and Configure Apache2

How to Install and Configure Apache2

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

By 
Ram Lakshmanan user avatar
Ram Lakshmanan
DZone Core CORE ·
Mar. 20, 20 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
31.3K Views

Join the DZone community and get the full member experience.

Join For Free

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!

Java (programming language) Host (Unix) Web server Directory

Opinions expressed by DZone contributors are their own.

Related

  • How Java Servlets Work: The Backbone of Java Web Apps
  • Multiplatform Directory Bookmarks on the Command Line
  • Java Z Garbage Collector (ZGC): Revolutionizing Memory Management
  • How To Build a Google Photos Clone - Part 1

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!