Apache Web Server: Installation (Linux)

You might want to set up a web server of your own for many reasons. Maybe you are starting to code websites using JavaScript and PHP, which is not generally supported without special software. Or you might want simply to have the collaborative abilities provided by a centralized web server such as allowing others to view your work from their own computers. There are many different choices available for web server software and the platform on which to run it. The most popular of these is Apache Web server under Linux, particularly Ubuntu Server. This tutorial explains the process of installing and setting up Apache web server as well as MySQL and PHP under the Linux operating system, hopefully keeping everything simple and easy to follow.

[installing] Apache Web Server

To start off with the installation, make sure your Internet is functional and open up the Terminal. As you might expect, we are going to have to jump right into some commands. We will start off by installing, of course, the Apache2 Web server. Please note that these commands are specifically for Ubuntu distributions, so you may have to replace some parts of commands such as apt-get with the counterpart available within your distribution.

sudo apt-get update

This command will take a few minutes and output a lot of text, but feel free to simply clear the terminal before continuing. It is also worthwhile to note that before installing any new software, you should run this command so that your computer can get an update as to what software is available at the time. Next, we actually install the web server.

sudo apt-get install apache2

This will then prompt for your password, enter it and press enter. Then a prompt will come up asking whether you wish to continue, enter a capital Y and press enter.

Open up your web browser, and enter localhost. If you are running this on a remote server on your local network, enter the IP address of the remote computer instead. You should see the default “It Works!” page. This means that the installation was successful, and our new server is already up and running.

Commands

There are a few commands that are important to know about. Most important, are those to start, restart, and stop Apache. There are four methods to do this which are outlined below.

Method 1: Init Script (Debian and Ubuntu Linux)

  • Start: sudo /etc/init.d/apache2 start
  • Restart: sudo /etc/init.d/apache2 restart
  • Stop: sudo /etc/init.d/apache2 stop

Method 2: Service (most Linux Distributions)

  • Start: sudo service apache2 start
  • Restart: sudo service apache2 restart
  • Stop: sudo service apache2 stop
  • Reload (configuration files): sudo service apache2 reload

The reload option is one that you might not be familiar with. It is much like the restart function, but it only restarts the required configuration files. This, as we will see later, is a very useful function for when you want to update the configuration files yet not have the server become unavailable for even that short time.

Method 3: Upstart (newest release of Ubuntu)

  • Start: sudo start apache2
  • Restart: sudo restart apache2
  • Stop: sudo stop apache2
  • Reload: sudo reload apache2

Method 4: apche2ctl (any Linux or UNIX Distribution)

apache2ctl is the built in Apache command set for performing these functions. It functions on any Linux or UNIX based OS since it is a tool that comes with Apache.

  • Start: sudo apache2ctl start
  • Restart: sudo apache2ctl restart
  • Stop: sudo apache2ctl stop
  • Reload: sudo apache2ctl reload

[installing] Additional Software

Now that we have Apache installed and know all the basic commands, it is time to install the extras: PHP5 and MySQL Server. The latter of these is a very useful SQL Database, required by most Content Management Systems (CMS) such as WordPress. PHP is used for interacting with the SQL database, and can also be used for many other applications. We will also install PHPmyAdmin – a web based front end to MySQL allowing easier database management.

PHP

Most newer Linux distributions, such as Ubuntu 16.10, come with PHP7 preinstalled, so we just need to install the mod (modification) allowing Apache to use PHP.

sudo apt-get install libapache2-mod-php7

However, if you are running an older system or simply want PHP5 rather than PHP7, run the same command except including php5 and changing the mod candidate:

sudo apt-get install php5 libapache2-mod-php5

Once you run the command, enter Y to continue. That’s it! PHP has been installed and should be functioning. However, it is always good to test and make sure that PHP is working properly, and we can do that by giving Apache some PHP code to execute. We can simply type the following line anywhere in the terminal to test PHP:

php -r 'echo "\n\nYour PHP installation is working fine.\n\n\n";'

This will print Your PHP installation is working fine. Now that PHP is functional, we can move on to MySQL!

MySQL

We will need MySQL to be able to interact with databases, and use CMS software such as WordPress. Assuming you are running PHP7, we must install php7.0-mysql to give PHP the ability to function along side MySQL, as well as the normal MySQL Server packages and the mod to work with out Apache server.

sudo apt-get install mysql-server php7.0-mysql phpmyadmin

During the installation you should be prompted to enter a root password for MySQL. You can always change or set this up later, though it is not the most convenient.

A prompt should also be shown asking you to configure phpmyadmin. Use the arrow keys to move between options, and the space to select an option. Generally, two options will be shown.

Web server to reconfigure automatically:
[ ] apache2
[ ] lighttpd

Simply make sure the red selector is over apache2, and press space to select. Then press Tab, and Enter to continue. Next you will be asked whether you want PHPmyAdmin to configure the initial database for you. Unless, as the prompt warns, you are an advanced database administrator, use the arrow keys to select the Yes option and Enter to continue.

You will be prompted a final time to enter a password for the database used by PHPmyAdmin. This password will not generally be needed, and could be the same, but I would recommend you enter a different password and write it down if needed. Press Tab and Enter to continue, then confirm your password and pres Tab then Enter once again.

That’s it! All the needed packages have been installed. To test PHPmyAdmin, open your browser and go to http://localhost/phpmyadmin. Some users have the issue that PHPmyAdmin prints out an error saying The mbstring extension is missing. Please check your PHP configuration. If this happens, run the following commands (separately).

sudo apt-get install php-mbstring php7.0-mbstring php-gettext
sudo service apache2 restart

This will install the mbstring extension for PHP, and restart the server to reinitialize the services allowing the new extension to function properly. You can optionally use a different restart command, such as one from the commands shown here.

You can now reload the PHPmyAdmin page, and log in with root and the password you set during the initial MySQL configuration.

Where Next?

Simply setting up the full LAMP (Linux Apache MySQL PHP) server is not the end. Stay tuned for more frequent posts, explaining many other details of Apache as well as other Linux programs and tech news.

On May 8th, you will learn how to edit, remove, and add pages to your new Apache Web Server.

Tagged with:

Leave a Reply

%d bloggers like this: