Install and Configure Apache on a Linux PC (as localhost)

How to install and setup an Apache2 web server on a Linux PC to serve content on localhost (127.0.0.1). A brief and concise stepwise guide.


This is a no-nonsense, straight-to-the-point guide on how to install Apache2 on your local Linux PC.

Apache web server is mostly used by web hosting companies. It has many options that are not at all relevant on a PC for personal use. There are many easier and more lightweight alternatives. You should really only choose Apache2 if you need the same environment locally as you have for your web site(s).

Enough words already.


(Edition 6)



Below, "#" means: Enter the rest of this line in a terminal (without the #-sign)


Install Apache2 web server

To install Apache2 and the module that supports PHP (skip line 2 if you don't need PHP):

# sudo apt install apache2 apache2-suexec-pristine
# sudo apt install libapache2-mod-php

Above will not install PHP, only add support for it in Apache. In "The Useful Linux After-Install Todo List" there is a section that will show you how to install PHP (and a database).

Configure Apache2 (= server configuration)

URL redirects and -rewrites

Enable PHP and URL-rewriting (or skip if you don't need it), then restart the Apache server to make it work:

# sudo a2enmod php
# sudo a2enmod rewrite
# sudo systemctl restart apache2

- - -

URL rewriting has to be allowed, even after enabling it. Open Apache2 config file in the nano text editor (you will need sudo to edit):

# sudo nano /etc/apache2/apache2.conf

Search for the relevant section: Use the key combination [ctrl]+[w] to activate the search field then enter:

"<Directory /var/www" followed by [enter]

To allow the use of ".htaccess" files in sub-folders (eg. for URL rewriting), change "AllowOverride" from "None" to "All" for the Directory "/var/www". For now, just keep the default "Options" "Indexes FollowSymLinks", and keep the default acces permission "Require all granted". When done it should look something like this:

    <Directory /var/www>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

Save with [ctrl]+[o], and exit nano with [ctrl]+[x]. Then restart the Apache server to make it work:

# sudo systemctl restart apache2

Index file order

The index file list determines what files Apache will send to the browser if you request a folder name without requesting a file. This is what happens eg. when you request a root domain like "http://example.com/".

If you mainly use one specific file type it is an advantage to put that file type first in the list.

To make Apache use, say "index.php" as default in stead of "index.html", open the Apache2 directory config file in the nano text editor (you will need sudo to edit):

# sudo nano /etc/apache2/mods-enabled/dir.conf

In the line starting with "DirectoryIndex", reorder the file names so that "index.php" is the first file name after "DirectoryIndex" (you may have to add it).

Save with [ctrl]+[o], and exit nano with [ctrl]+[x].

- - -

Restart the Apache server to apply your changes:

# sudo service apache2 restart

Edit default Apache2 site configuration (= localhost configuration)

The Apache2 configuration above is for the entire server. Above, the "AllowOverride All" allowed the use of individual configurations for subfolders of "/var/www". Now, repeat this configuration process for the main subfolder "/var/www/html". This folder will appear as "http://localhost" in your browser.

- - -

First check if a default site configuration is enabled. Then, before editing, take a look at the default site configuration. It will probably be called "000-default.conf".

# ls /etc/apache2/sites-enabled
# cat /etc/apache2/sites-enabled/000-default.conf

If the output is not empty it is probably just as it should be.

- - -

Open the deafult site configuration in the nano text editor (you will need sudo to edit):

# sudo nano /etc/apache2/sites-enabled/000-default.conf

After/below "DocumentRoot /var/www/html" enter:

        <Directory /var/www/html>
            Options Indexes FollowSymLinks
            AllowOverride All
            Order allow,deny
            Allow from all
        </Directory>

- - -

Restart the Apache server to apply your changes:

# sudo service apache2 restart

Add your username to the group "www-data"

First, check if you are in that group already:

# groups username

(Replace "username" with your user name)

If not:

# sudo usermod -a -G www-data username

(Still replace "username" with your user name)

Note: The group add may not be fully in place across all subsystems until after the next reboot.

Configure localhost folders

Set user and group (the folder "html" inside your "www" folder is "http://localhost/" in your browser):

# sudo chown -R www-data:www-data /var/www/

- - -

Then, set permissions:

# sudo chmod -R ug=rwx /var/www

... or (another way of doing the exact same thing):

# sudo chmod -R 770 /var/www/

- - -

Optional: If you have installed phpMyAdmin, link it into your "www/html" folder. This guide does not explain how to install phpMyAdmin, so if you have not installed it separately just skip this step

# sudo ln -s /usr/share/phpmyadmin /var/www/html/phpmyadmin

- - -

Control that you have done what you think you did:

# ls -lh /var/www/

As you have allowed Apache2 to "FollowSymLinks" you may now link content from your home folder into localhost by using a symbolic link.

Say you have a web site saved in the folder "~/website". Then you can easily make it so that any edit that you make in the "~/website" folder will show up in your browser at "http://localhost/website/". That way you do not need to navigate all the way to "/var/www/html/website/" each time you need to make an edit. Do:

# ln -s /home/username/website /var/www/html/website

(NOTE: Above, "username" should be your user name, replace it first.
Also, replace "website" with the name you chose for your folder.)

- - -

Control that you have done what you think you did:

# ls -lh /var/www/html

- - -

Next, change ownership of the local folder (the one in the "/home/" directory):

You may need to, eg. write a file to the folder at some point. If permissions are not set right, you will not be able to do that. So:

# sudo chown -R username:www-data /home/username/website/

(Still replace "username" with your user name, and "website" with your chosen folder name)

This makes the folder owned by "username", and it makes the folder part of the group "www-data" which will allow your web server to save a file to the folder.

To make sure that the group as well as the user has sufficient permissions, enter this in the terminal:

# sudo chmod -R 770 /home/username/website/

The above command makes sure that both the user and the group are able to read, write, and execute programs in you local folder. That way both you and your web server, DB-manager, PHP, etc will be able to perform all operations on the folder, and you will avoid some errors.

- - -

Control that you have done what you think you did:

# ls -lh /home/username/website/

(Still replace "username" with your user name)

The first colum should look like this for each subfolder:

drwxrwx---

Set up Apache virtual hosts (on localhost)

If you want to be able to reach your local web site in a browser by typing "http://website/" in stead of the longer "http://localhost/website/" you will need to set up a "Virtual Host".

The process for doing so tends to change a little with some new versions of Apache, so use the official documentation:

A search engine may also be useful. A guide is outside the scope of this text, but it is not extremely complicated to do. If you could complete this page you can also set up a Virtual Host.


APPPENDIX: A few ways to reload and restart Apache2

Reload

# sudo service apache2 reload

- or:

# systemctl reload apache2

- - -

Restart

# sudo service apache2 restart

- or:

# sudo systemctl restart apache2

- or:

# sudo /etc/init.d/apache2 restart

- - -

Check status of Apache:

# sudo systemctl status apache2


This list has been broken out of the Linux after-install todo list (the useful list) in order to reduce the size of it. And then because it is a useful list in-and-by-itself.


 

 


Document URL: https://clsc.net/articles/apache-linux-localhost.php