Wednesday, 1 January 2014

How-to: Install LaMp Stack on Debian 7

If you remember a while back, I installed the LaMp stack on Linux in preparation for installing Wordpress... which I did but didn't post the How-to. Err, sorry about that. I'll get around to it, promise.

Well, that LaMp How-to was a tad simple-minded and things have moved on a little. Here's an update with a bit more detail to it. You'll note I'm doing this the old fashioned way, step by step, command line by command line. We'll look at an easier way soon.

L is for Linux
I'm using Debian these days, but you can as easily choose Ubuntu server, Arch, Gentoo or whatever. Before starting to install the LaMp stack, make sure your chosen Linux distribution is up to date. You can do this through the system tools, Software Update, Software Centre, or if you must, in a Root Terminal using good old aptitude (the '#' prompt indicates commands to issue as root):

 # aptitude update && aptitude upgrade

A is for Apache2
Apache is the web server, which can be installed as follows:

 # aptitude install apache2 apache2-doc

To configure user directories for the Apache Web Server, run the Enable module

# a2enmod userdir

You should get the message:
 To enable the new configuration you need to run
# service apache2 restart

This is the usual command to restart Apache, so do that:
 # service apache2 restart

Next configure the Apache module userdir in /etc/apache2/mods-enabled/userdir.conf as follows:

<IfModule mod_userdir.c>
        UserDir public_html
        UserDir disabled root

        <Directory /home/*/public_html>
                AllowOverride All
                Options MultiViews Indexes SymLinksIfOwnerMatch
                <Limit GET POST OPTIONS>
                        Order allow,deny
                        Allow from all
                <LimitExcept GET POST OPTIONS>
                        Order deny,allow
                        Deny from all

Create directory as a normal user (not as root):

$mkdir /home/$USER/public_html

Change group as root (substitute your username) and restart web server:

# chgrp www-data /home/<username>/public_html
# service apache2 restart

If you get a Forbidden error when accessing home folder through Apache, check /home/username has the permissions drwxr-xr-x. If the permissions are wrong correct them as such:

# chmod 755 /home/<username>

M is for MySQL
Next install the MYSQL database server and client using the following command:

 # aptitude install mysql-server mysql-client

In Debian 7 and most current Linux versions, you will be asked to input MySQL root user's password during installation. If not, then immediately after you have installed the MySQL server, you should change its root password.

 # /usr/bin/mysqladmin -u root password 'enter-new-password'

Of course, remember you should only use the root account for normal running of databases; root is a privileged account which should only be used for administrative tasks. The MySQL installer normally creates a separate user account to connect to your MySQL databases from a PHP script. If the command line is too much for you, a control panel like phpMyAdmin is a convenient tool to create or assign database permissions for users.

P is for PHP

Installing the PHP layer of LaMp in Debian can be done with one command in the root terminal:

 # aptitude install php5 php5-mysql libapache2-mod-php5

At this point in Debian Wheezy I got the message:
The following packages have unmet dependencies:
 apache2-mpm-prefork : Conflicts: apache2-mpm which is a virtual package.
 apache2-mpm-worker : Conflicts: apache2-mpm which is a virtual package.
The following actions will resolve these dependencies:

     Remove the following packages:
1)     apache2-mpm-worker         

Accept this solution? [Y/n/q/?]

If you get this, don't panic. I accepted with the Y option and Debian resolved the dependencies.

In order to serve PHP, the /etc/apache2/mods-available/php5.conf needs to be correct with the following settings:

<IfModule mod_php5.c>
    <FilesMatch "\.ph(p3?|tml)$">
        SetHandler application/x-httpd-php
    <FilesMatch "\.phps$">
        SetHandler application/x-httpd-php-source
    # To re-enable php in user directories comment the following lines
    # (from <IfModule ...> to </IfModule>.) Do NOT set it to On as it
    # prevents .htaccess files from disabling it.
    #<IfModule mod_userdir.c>
    #    <Directory /home/*/public_html>
    #        php_admin_value engine Off
    #    </Directory>

Again, if the installer is up to date, you may not need to do anything.

Place some web content in ~/public_html and see the results at http://localhost/~username

where ~username is your regular (not root) username for which we configured user directories in the web server above.

Apache2 Configuration
It used to be that you had to edit the Apache2 configuration file, /etc/apache2/apache2.conf, with a bunch of complex stuff. In most cases, the server confiuration is now done through conf.d.

Testing PHP
To test the PHP interface, edit the file /var/www/apache2-default/test.php:
 # gedit /var/www/apache2-default/test.php

and insert the following code:
<?php phpinfo(); ?>

Point your browser to http://localhost/apache2-default/test.php and you should get the info page for the installed PHP version.

Another P for phpMyAdmin

As mentioned earlier, the phpMyAdmin tool is useful for easy configuration:
 # aptitude install phpmyadmin

The one option the installer comes up with is:
Server to configure
with the choices
Apache2 or LightPPD
which should be obvious unless you have installed the lightweight LightPPD.

The installer should configure phpMyAdmin for your website automatically.

Try it by invoking http://localhost/phpmyadmin/

You will need to restart Apache to complete the install of phpMyAdmin:
 # /etc/init.d/apache2 restart

In the first-run setup of phpMyAdmin, you can configure the database for phpmyadmin with dbconfig-common ('Yes') for a simple setup.

You will also need to use the MySQL admin password in order to set the phpMyAdmin password.

That should give you a complete LaMp stack on Debian 7. At least, that's what it gave me, ready to install a content management system. RC

No comments:

Post a Comment

At least try to be nice, it won't kill you...