How to install Apache, PHP and MYSQL
In order to install all those services you need to use the package manager installed in your system. In this case we will
be using apt-get because is the package manager in Ubuntu. If it was a RedHat system like CentOS you should use
yum
First we will update the list of packages cached in the system. If yum was used this step would not be necessary because yum does not keep a local cache of the available packages, it always looks in the repositories for the latest software available.
root@sh-srv:~# apt-get update
The next packages are needed
- apache2
- php5
- libapache2-mod-php5
- mysql-server
- mysql-client
- php5-mysql
This task can be accomplish by using the 'install' argument [apt-get install 'package1'
'package2'].
root@sh-srv:~# apt-get install apache2 php5 mysql-server mysql-client libapache2-mod-php5 php5-mysql -y
The '-y' switch is optional.
Checking apache
First you need to know if the apache web server daemon is running, there are different ways to find out depending on the distribution. In RedHat like systems the apache daemon is called "httpd" and in debian like systems it is called "Apache2"
In Ubuntu it would be like this:
root@sh-srv:~# etc/init.d/apache2 status
root@sh-srv:~# service apache2 status
If the webserver is running you should see a message like this:
Checking file permissions and document root
The document root for the website has to be readable and executable by everybody. By default it is either '/var/www' or '/var/www/html' depending of the distribution. To find out what is the document root check the apache configuration file.
It is important to know what is the name of the web server user, to find out use the next command:
root@sh-srv:~# ps aux | egrep '(apache|httpd)'
It is usually "Apache" or "www-data". All web server files should be owned by this user, to set it that way use the next commands:
root@sh-srv:~# chmod 755 /var/www -R
root@sh-srv:~# chown www-data. /var/www -R
The '-R' switch means 'recursive'. That will change the permissions for all files recursively
To find out if apache is running open a web browser and type http://localhost/ or http://{ip addr.}/ or http://{hostname}/
Checking PHP
Create a txt file using a text editor, type the text below, save it as 'index.php' and put it in the document root.
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php phpinfo(); ?>
</body>
</html>
Then open a web browser and type the ip address of the machine where you have installed apache and php, in my case the ip is 192.168.1.46. You should see
something like this:
Checking MySQL
To check if mysql is working just connect to the database using the next command:
root@sh-srv:~# mysql -u 'username' -p
'-u' for username
'-p' for password
You should type the username and password that you created during the installation. If it works you should not get any
error messaged, the result should be a prompt like this:
© 2025 Julian's Corner. All rights reserved