Menu Close

Installing Apache2 With PHP5 And MySQL Support On Ubuntu 12.04 LTS (LAMP)

by howtoforge.com

LAMP is short for Linux, Apache, MySQL, PHP. This tutorial shows how you can install an Apache2 webserver on an Ubuntu 12.04 LTS server with PHP5 support (mod_php) and MySQL support.

I do not issue any guarantee that this will work for you!

 

1 Preliminary Note

In this tutorial I use the hostname server1.example.com with the IP address 192.168.0.100. These settings might differ for you, so you have to replace them where appropriate.

I’m running all the steps in this tutorial with root privileges, so make sure you’re logged in as root: 

sudo su 

2 Installing MySQL 5

First we install MySQL 5 like this:

apt-get install mysql-server mysql-client

If you want to install mysql5.6 instead of mysql5.5 you have to run only (works in ubuntu 14 and higher):

apt-get install mysql-server-5.6

You will be asked to provide a password for the MySQL root user – this password is valid for the user root@localhost as well as root@server1.example.com, so we don’t have to specify a MySQL root password manually later on:

New password for the MySQL “root” user: <– yourrootsqlpassword
Repeat password for the MySQL “root” user: <– yourrootsqlpassword

 

3 Installing Apache2

Apache2 is available as an Ubuntu package, therefore we can install it like this:

apt-get install apache2

Now direct your browser to http://192.168.0.100, and you should see the Apache2 placeholder page (It works!):

Apache’s default document root is /var/www on Ubuntu, and the configuration file is /etc/apache2/apache2.conf. Additional configurations are stored in subdirectories of the /etc/apache2 directory such as /etc/apache2/mods-enabled (for Apache modules), /etc/apache2/sites-enabled (for virtual hosts), and /etc/apache2/conf.d.

 

Apache 2.4.3 (or maybe slightly earlier) added a new security feature that often results in 403 Forbidden error. You would also see a log message of the form “client denied by server configuration”. The feature is requiring a user identity to access a directory. It is turned on by DEFAULT in the httpd.conf that ships with Apache. You can see the enabling of the feature with the directive

Require all denied

This basically says to deny access to all users. To fix this problem, either remove the denied directive (or much better) add the following directive to the directories you want to grant access to:

Require all granted

as in

<Directory "your directory here">
   Order allow,deny
   Allow from all
   # New directive needed in Apache 2.4.3: 
   Require all granted
</Directory>

 

4 Installing PHP5

We can install PHP5 and the Apache PHP5 module as follows:

apt-get install php5 libapache2-mod-php5

We must restart Apache afterwards:

/etc/init.d/apache2 restart

 

5 Testing PHP5 / Getting Details About Your PHP5 Installation

The document root of the default web site is /var/www. We will now create a small PHP file (info.php) in that directory and call it in a browser. The file will display lots of useful details about our PHP installation, such as the installed PHP version.

vi /var/www/info.php
<?php
phpinfo();
?>

Now we call that file in a browser (e.g. http://192.168.0.100/info.php):

As you see, PHP5 is working, and it’s working through the Apache 2.0 Handler, as shown in the Server API line. If you scroll further down, you will see all modules that are already enabled in PHP5. MySQL is not listed there which means we don’t have MySQL support in PHP5 yet.

 

6 Getting MySQL Support In PHP5

To get MySQL support in PHP, we can install the php5-mysql package. It’s a good idea to install some other PHP5 modules as well as you might need them for your applications. You can search for available PHP5 modules like this:

apt-cache search php5

Pick the ones you need and install them like this:

apt-get install php5-mysql php5-curl php5-gd php5-intl php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-ming php5-ps php5-pspell php5-recode php5-snmp php5-sqlite php5-tidy php5-xmlrpc php5-xsl

Now restart Apache2:

/etc/init.d/apache2 restart

Now reload http://192.168.0.100/info.php in your browser and scroll down to the modules section again. You should now find lots of new modules there, including the MySQL module:

7 phpMyAdmin

phpMyAdmin is a web interface through which you can manage your MySQL databases. It’s a good idea to install it:

apt-get install phpmyadmin

You will see the following questions:

Web server to reconfigure automatically: <-- apache2
 Configure database for phpmyadmin with dbconfig-common? <-- No

Afterwards, you can access phpMyAdmin under http://192.168.0.100/phpmyadmin/:

 

By default, phpMyAdmin installs its web interface to a well-known location, like yourhost.com/phpMyAdmin. In order to make the phpMyAdmin installation more secure, it is a good idea to change this to a different, unique name. In order to do so, open the apache.conf file in the phpMyAdmin’s configuration directory. On many Linux servers, this file is located in/etc/phpmyadmin/. At the top of apache.conf, look for the following line:

# phpMyAdmin default Apache configuration

Alias /phpmyadmin /usr/share/phpmyadmin

Simply change the alias to point to the unique URL you want for your installation. For example:

# phpMyAdmin default Apache configuration

Alias /secretadmin /usr/share/phpmyadmin

After this is done, restart the Apache service to see the changes:

sudo /etc/init.d/apache2 restart

Now visit the new URL, and phpMyAdmin should be there. For example, yoursite.com/secretadmin.

If phpmyadmin tell you that mcrypt extension is missing, you can try this:

sudo apt-get install php5-mcrypt
sudo ln -s /etc/php5/conf.d/mcrypt.ini /etc/php5/mods-available
sudo php5enmod mcrypt 
sudo service apache2 restart

If you want to enable phpmyadmin for SSL only  you can add this line at bottom of /etc/phpmyadmin/config.inc.php

$cfg['ForceSSL'] = 'true';

8 Links

Posted in Linux, News, Web Development

Leave a Reply

Your email address will not be published. Required fields are marked *