Share via:

Installing the Apache HTTP Server in Linux

Hot to install the Apache HTTP server in Linux.

378 views

Edited: 2018-02-25 05:00

install Apache, Ubuntu

This tutorial shows how to install the Apache HTTP Server from a terminal in Ubuntu or Debian. After following the instructions in this tutorial, you should be able to run a local web server.

If you want your web server to be accessible from the internet, more steps are usually needed. For example, if you are behind a router or firewall you may need to open up a port and point it to the IP address of the local server (I.e. 10.0.0.x). It is also a good idea to manually connect your server to the LAN, so that you can maintain a dedicated LAN IP.

You can use the apt-get command with sudo. I.e.

sudo apt-get install apache2

Running the above command will ask us for our root password, since we are running it with sudo. The install part should be self-explanatory.

When it is done installing, you should be able to test if it works by entering http://localhost/ in your browser, a standard Apache page is shown if the server has successfully been installed.

If you later want to remove the Apache, simply replace the install part with remove. I.e.:

sudo apt-get remove apache2

Testing if it works and final tips

To test if the installation was successful, you may type in http://localhost/ in your browser. If the server was successfully installed you should now see a standard greeting page by Apache. The location of this page is /var/www/

The www directory can be used to keep all of your website files. If you are running Apache as a test server, it is usually a good idea to add your own user to the www-data group, and then chmod the permissions of the www directory to 775, as it will allow you to change files without first changing to the root user.

useradd -G [group-name] [username]

Set the Apache user (www-data) as the owner of the www directory:

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

Set permissions to 775 (user and group has full access, everyone else, only read and execute):

sudo chmod -R 775 /var/www

The -R flag means recursive, making it also change permissions of subdirectories.

Starting and Stopping the server

To start and stop the server, you can try using the following terminal commands.

sudo service apache2 start
sudo service apache2 stop
sudo service apache2 restart

In case the above commands are now working for you, try the below instead:

sudo /etc/init.d/apache2 start
sudo /etc/init.d/apache2 stop
sudo /etc/init.d/apache2 restart

If none of the above is working, try using systemctl instead:

sudo systemctl apache2 start
sudo systemctl apache2 stop
sudo systemctl apache2 restart

Comments