First step is to logging into your linux shell. For this tutorial I am using a VPS (A Digital Ocean Droplet running Ubuntu 16.04). I have opened an ssh connection to server using Bitvise SSH client.
Having logged in, first run below command to get an updated list of packages from your system’s software repositories:
sudo apt-get update
This will retrieve the updated list of packages from official Ubuntu software repository as well as from any third party PPAs that have been added as repository sources.
I have used ‘sudo’ before the apt-get command. This is because I have logged in as a non-root user and I am trying to execute a command that needs root permissions. ‘sudo’ is a safe way to execute commands that needs root permissions without having to login as the root user. To use ‘sudo’ your non-root user account have to be added into the sudoers group. When running a command with ‘sudo’ you’ll be asked to type your logged in account’s password.
Once ‘apt-get update’ finishes retrieving the packages you’ll be returned to your shell prompt. Now you can run below command to install Apache on your system. It is available from the official repository as a package named ‘apache2’
sudo apt-get install apache2
It will show you what packages will be installed and how much disk space will be required. When asked for the confirmation Type Y and hit enter to proceed with the installation. You should see the installation progress with running commands and output. This won’t take long. At the end you’ll be returned to your shell prompt.
Now Apache should be installed. To verify the installation try running below command:
sudo systemctl status apache2
You should see an output similar to following:
Notice the green text underlined in red. It means the Apache server is installed and started.
Now that you have got Apache installed and running on your system, its important to learn few commands that will help you with stopping, starting, and restarting the server when necessary.
Stopping the service:
If the server is already running enter below command to stop it:
sudo systemctl stop apache2
Now go ahead and check the status:
sudo systemctl status apache2
Its output should look similar to what you can see in the below screenshot. The text underlined in red shows the service is not running at the moment:
Start the service again:
Now that the service has stopped. We definitely gonna need a way to start it again. Below command does exactly that:
sudo systemctl start apache2
If you check the status you will see the same status message you saw in above FIGURE 1.
Restart the service:
Restart performs two actions. First it stops the service if it is already running. Then starts it again. This saves you from having to manually stop and start the service with two separate commands. When you make any changes to the Apache’s configuration and if the service is already running, you must restart the service for those changes to take effect. In between the time of stopping and restarting, any website(s) running on that Apache server will be unavailable to its users.
sudo systemctl restart apache2
Reload the configuration:
Below command will force a running Apache service to read its new configuration without affecting its current connections. This saves you from having to restart the service when you need to apply new configuration changes.
sudo systemctl reload apache2
Disable the Service from starting Automatically:
When you install Apache, by default it will be configured to start automatically when the system boots up. If you don’t want that to happen, you can disable this behavior by running the below command:
sudo systemctl disable apache2
If you do this however, you will have to start the service manually every time the system is restarted.
Make the Service start Automatically:
If you have disabled the service from starting automatically or if it is not already configured to do so, you can enable it by running the below command:
sudo systemctl enable apache2
This will make the service start automatically on each time the system is restarted.