Installing Apache2 and PHP on Debian/Ubuntu Systems

Browsers do not have a built-in interpreter for server-side scripts written in PHP; they require a web server to interact with a PHP interpreter program.

The two most popular ones (open source) are Apache and Nginx. For the purpose of this tutorial, we choose Apache.

1) Install the Apache HTTP Server

We first fetch the system's packages and update them to their newer versions

					
						sudo apt-get update
					
				

In Debian/Ubuntu systems, the Apache HTTP Server (v. 2.0) can be installed by typing the below command at the terminal

					
						sudo apt-get install apache2
					
				

Check the version number to verify if it is installed properly

					
						apache2 -v
					
				

2) Start Apache

The Apache web server can be started by typing the below command into the terminal

					
						sudo /etc/init.d/apache2 start
					
				

After starting the server, type http://localhost in the URL bar of your web browser. It should display the Apache2 Ubuntu Default Page shown below

apache2 installation in ubuntu
Apache2 Ubuntu Default Page

The command for restarting Apache 2 is similar to starting it. Just run the command

					
						sudo /etc/init.d/apache2 restart
					
				

and for stopping, run

					
						sudo /etc/init.d/apache2 stop
					
				

3) Install PHP

To install PHP (v. 5), we run the command

					
						sudo apt-get install php5
					
				

We also install the libapache2-mod-php5 package which provides the PHP5 module for Apache 2.

					
						sudo apt-get install libapache2-mod-php5
					
				

Verify the installation by checking the PHP version

					
						php -v