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 2)



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

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/

Configure Apache2 (= server configuration)

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

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].

- - -

To make apache use "index.php" as default in stead of "index.html", open 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

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/". Do:

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

(NOTE: Above, "username" should be your user name, replace it first)

- - -

Control that you have done what you think you did:

# ls -lh /var/www/html

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 guide was taken out of the Linux After-Install Todo List (the useful list) in order to reduce the size of it.


 

 


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