Install and Configure Apache

Key Value
Summary This tutorial covers the installation and configuration of an Apache web server
Categories server
Difficulty 3
Author Aden Padilla adenpadilla@gmail.com

Overview

Duration: 1:00

Apache is an open source web server that’s available for Linux servers free of charge.

In this tutorial we’ll be going through the steps of setting up an Apache server.

What you’ll learn

  • How to set up Apache
  • Some basic Apache configuration

What you’ll need

  • Ubuntu Server 16.04 LTS
  • Secure Shell (SSH) access to your server
  • Basic Linux command line knowledge

Got everything ready? Let’s move on to the next step!

Originally authored by Aden Padilla

Installing Apache

Duration: 1:00

To install Apache, install the latest meta-package apache2 by running:

sudo apt update
sudo apt install apache2

After letting the command run, all required packages are installed and we can test it out by typing in our IP address for the web server.

If you see the page above, it means that Apache has been successfully installed on your server! Let’s move on.

Creating Your Own Website

Duration: 4:00

By default, Apache comes with a basic site (the one that we saw in the previous step) enabled. We can modify its content in /var/www/html or settings by editing its Virtual Host file found in /etc/apache2/sites-enabled/000-default.conf.

We can modify how Apache handles incoming requests and have multiple sites running on the same server by editing its Virtual Hosts file.

Today, we’re going to leave the default Apache virtual host configuration pointing to www.example.com and set up our own at gci.example.com.

So let’s start by creating a folder for our new website in /var/www/ by running

sudo mkdir /var/www/gci/

We have it named gci here but any name will work, as long as we point to it in the virtual hosts configuration file later.

Now that we have a directory created for our site, lets have an HTML file in it. Let’s go into our newly created directory and create one by typing:

cd /var/www/gci/
nano index.html

Paste the following code in the index.html file:

<html>
<head>
  <title> Ubuntu rocks! </title>
</head>
<body>
  <p> I'm running this website on an Ubuntu Server server!
</body>
</html>

Pretty cool, right?

Now let’s create a VirtualHost file so it’ll show up when we type in gci.example.com.

Setting up the VirtualHost Configuration File

Duration: 3:00

We start this step by going into the configuration files directory:

cd /etc/apache2/sites-available/

Since Apache came with a default VirtualHost file, let’s use that as a base. (gci.conf is used here to match our subdomain name):

sudo cp 000-default.conf gci.conf

Now edit the configuration file:

sudo nano gci.conf

We should have our email in ServerAdmin so users can reach you in case Apache experiences any error:

ServerAdmin yourname@example.com

We also want the DocumentRoot directive to point to the directory our site files are hosted on:

DocumentRoot /var/www/gci/

The default file doesn’t come with a ServerName directive so we’ll have to add and define it by adding this line below the last directive:

ServerName gci.example.com

This ensures people reach the right site instead of the default one when they type in gci.example.com.

Now that we’re done configuring our site, let’s save and activate it in the next step!

Activating VirtualHost file

Duration: 1:00

After setting up our website, we need to activate the virtual hosts configuration file to enable it. We do that by running the following command in the configuration file directory:

sudo a2ensite gci.conf

You should see the following output

Enabling site gci.
To activate the new configuration, you need to run:
  service apache2 reload
root@ubuntu-server:/etc/apache2/sites-available#

To load the new site, we restart Apache by typing:

service apache2 reload

End result

Now is the moment of truth, let’s type our host name in a browser. Final Hooray!

Further reading:

1 Like

In base to the request of @anthonydillon in the “Issues” [Install and Configure Apache] ‘This site can’t be reached’ in Google Chrome I have tried the tutorial and considered that the cause that cannot be “reached” is that it has not spelled out in the file /etc/hosts the text 127.0.1.1 gci.example.com causing it not to be visible in locally.

Therefore, I consider it necessary to add one more step to the tutorial where it is specified that it is necessary to add 127.0.1.1 gci.example.com to /etc/hosts

6 Likes

The second step in this Install and Configure Apache tutorial mentions “After letting the command run, all required packages are installed and we can test [whether Apache is installed] by typing in our IP address for the web server.” This works. Alternatively, a user may enter localhost in their browser address bar, press enter, and they should be able to test it that way too. This way may be a little more comfortable and quicker for beginners.

1 Like

Hi, I have followed these instructions and tried to do it a few times. I am not sure where I am going wrong. Localhost, even with 192 & 127 I could access the page but when I tried gci.example.com then I get a “server not found”. I am using Peppermint OS, could that be the issue?

2 Likes

After install (page2) I needed to start the server withb
systemctl start apache2

3 Likes

I also needed to type “systemctl start apache2”
Running Ubuntu 20.04
Server version: Apache/2.4.41 (Ubuntu)
Server built: 2020-08-12T19:46:17
The Apache version can be obtained from
“sudo apache2 -v”

I think the folder needs to be created in the /var/www/html/gci/ for apache to pick it up.

Right after the service apache2 reload command we need to register the website onto the local host. The os (windows, linux, mac and etc.) always checks the local host before querying the router or DNS servers to map an ip address to a domain name :).

  1. Run sudo nano /etc/hosts
  2. Under 127.0.0.1 localhost add 127.0.0.2 gci.example.com
  3. open a browser and search gci.example.com :slight_smile:
3 Likes

These needed to be added:

Permissions changes commands:
" sudo chmod a+rwx /ect/apache2/sites-available/html.conf | sudo chmod a+rwx /ect/apache2/sites-available/000-default.conf | sudo chmod a+rwx /etc/apache2/ports.conf ".

After adding line " Listen 8081" to ports.conf file under " Listen 80 ", the site is now visible!

1 Like

Do you know, what you are doing?

1 Like

I have a rough idea.
If I’m doing something wrong, let me know.

I had to give myself permissions to these files so that I could edit them…

index.html
hosts
ports.conf
000-default.conf
html.conf

I am using Virtual Box for this environment along side Ubuntu, Kali and Free NAS.
All in virtual.

1 Like

Having a rough idea is not enough to propose changes to a tutorial of a web server.

You need to learn the basics about permissions, owner, groups and others.

No, you don’t need to give yourself permissions. Apache is a web server: one part is running as user root and most processes are running as user www-data. Every file is either owned by root or www-data.

You as a (human) user don’t need to have any permissions there. If you have to change something, you can do it with elevated privileges. And normally you are not working directly there, but on your client machine and then you are deploying the changes on the web server.

With chmod a+??? you give the same set of permissions to all: to the owner of the file, to the group of the file and to everyone else. And everyone else means to every process and every human user on the system.

You gave the permissions rwx:

  • r seems reasonable. The web server needs to read configuration files. If you have some private key or some configuration file with a password inside, think twice about who should be able to read the file.
  • w is reasonable for users, which should write on the file. Typically, this right is limited to root.
  • x is not reasonable. There is no good reason, why a configuration file should be executable.

You mentioned, that you have Kali installed. Good, you can try it out. Hack your web server. Try to gain as much information as you can. Use some vulnerability scanner. Gain access to the system. Make a privilege escalation.

2 Likes

the above 3 steps helped…thank you very much

1 Like

I ran into the problem of not being able to resolve the gci.example.com page.
I tried many things, but the persistent solution online appears to be edit the localhost file with this entry 127.0.0.1 gci.example.com
I tried editing the server’s host file, no go reverted back to original
i tried editing my physical system’s host file, also no go, no go reverted back to original.
I finally got it to work by editing my physical system’s host file using the actual server IP address instead of a loopback address.

1 Like

This solution worked and my second site is loading in browser!

Thank you!

You need to put the site in /etc/hosts file like thi
127.0.1.1 gci.example.com
and you might need to disable the default conf file by running
sudo a2dissite 000-default.conf

This needs to be added to the tutorial, it didn’t work without these steps.

I have tried this several times and no matter what I do, the secondary site page does not come up. I have tried rebuilding the conf file twice and still no dice. I have no problem completing the steps, restarting apache, etc. However, the URL never comes up, just a “Page Not Found” error. I even opened a post in the Apache Subreddit to see if they know. https://www.reddit.com/r/apache/comments/zx2h8p/newbie_having_issues_with_virtual_sites/

In section 5. Activating VirtualHost file it asks you to run the command:

service apache2 reload

The command needs to be run under sudo:

sudo service apache2 reload

I had to remove the symbolic link /etc/apache2/sites-enabled/000-default.conf
then restart Apache for the gci test page to come up