Thursday, April 4, 2013

LAMP Server on Debian/Ubuntu based Distros


This guide provides step by step instructions for installing a full featured LAMP stack on an Ubuntu 12.10. In this guide, you'll learn how to set up Apache, MySQL, and PHP.

1. Set up the Hostname

Before you begin installing and configuring the components described in this guide, please make sure you've followed our instructions for setting your hostname. Issue the following commands to make sure it is set properly (The first command should show your short hostname, and the second should show your fully qualified domain name (FQDN)):

hostname
hostname -f


2. Install and Configure the Apache Web Server

Make sure your package repositories and installed programs are up to date by issuing the following commands:

apt-get update
apt-get upgrade --show-upgraded

To install the current version of the Apache web server (in the 2.x series) on an Ubuntu system use the following command:

apt-get install apache2

- Configure Name-based Virtual Hosts

There are different ways to set up virtual hosts, however we recommend the method below. By default, Apache listens on all IP addresses available to it.

First, create a file in the /etc/apache2/sites-available/ directory for each virtual host that you want to set up. Name each file with the domain for which you want to provide virtual hosting. See the following example configurations for the hypothetical "test.com" and "test.org" domains.

File:/etc/apache2/sites-available/test.com
<VirtualHost *:80>
     ServerAdmin webmaster@test.com
     ServerName test.com
     ServerAlias www.test.com
     DocumentRoot /srv/www/test.com/public_html/
     ErrorLog /srv/www/test.com/logs/error.log
     CustomLog /srv/www/test.com/logs/access.log combined
</VirtualHost>

File:/etc/apache2/sites-available/test.org
<VirtualHost *:80>
     ServerAdmin webmaster@test.org
     ServerName test.org
     ServerAlias www.test.org
     DocumentRoot /srv/www/test.org/public_html/
     ErrorLog /srv/www/test.org/logs/error.log
     CustomLog /srv/www/test.org/logs/access.log combined
</VirtualHost>

Notes regarding this example configuration:

All of the files for the sites that you host will be located in directories that exist underneath /srv/www You can symbolically link these directories into other locations if you need them to exist in other places.
ErrorLog and CustomLog entries are suggested for more fine-grained logging, but are not required. If they are defined (as shown above), the logs directories must be created before you restart Apache.
Before you can use the above configuration you'll need to create the specified directories. For the above configuration, you can do this with the following commands:

mkdir -p /srv/www/test.com/public_html
mkdir /srv/www/test.com/logs

mkdir -p /srv/www/test.org/public_html
mkdir /srv/www/test.org/logs

After you've set up your virtual hosts, issue the following commands to enable the sites:

a2ensite test.com
a2ensite test.org

This command symbolically links your virtual host file from sites-available to the sites-enabled directory. Finally, before you can access your sites you must reload Apache with the following command:

service apache2 reload

If you wanted to disable the test.com site, for example issue the following command:

a2dissite test.com

The a2dissite command is the inverse of a2ensite. After enabling, disabling, or modifying any part of your Apache configuration you will need to reload the Apache configuration again with the /etc/init.d/apache2 reload command. You can create as many virtual hosting files as you need to support the domains that you want to host.

- Testing Apache

Open up any web browser and then enter the following into the web address:

http://localhost/

You should see a message saying "It works!" , congrats to you! 

3. Install and Configure the MySQL Database Server


The first step is to install the mysql-server package, which is accomplished by the following command:

apt-get install mysql-server

During the installation you will be prompted for a password. Choose something secure (use letters, numbers, and non-alphanumeric characters) and record it for future reference.

At this point MySQL should be ready to configure and run. While you shouldn't need to change the configuration file, note that it is located at /etc/mysql/my.cnf for future reference.

- Configure MySQL and Set Up Databases

After installing MySQL, it's recommended that you run mysql_secure_installation, a program that helps secure MySQL. While running mysql_secure_installation, you will be presented with the opportunity to change the MySQL root password, remove anonymous user accounts, disable root logins outside of localhost, and remove test databases. It is recommended that you answer yes to these options. If you are prompted to reload the privilege tables, select yes. Run the following command to execute the program:

mysql_secure_installation

Next, we'll create a database and grant your users permissions to use databases. First, log in to MySQL:

mysql -u root -p

Enter MySQL's root password, and you'll be presented with a MySQL prompt where you can issue SQL statements to interact with the database.

To create a database and grant your users permissions on it, issue the following command. Note, the semi-colons (;) at the end of the lines are crucial for ending the commands. Your command should look like this:

create database anneke;
grant all on anneke.* to 'ximena' identified by '5t1ck';
flush privileges;

In the example above, anneke is the name of the database, ximena is the username, and 5t1ck password. Note that database user names and passwords are only used by scripts connecting to the database, and that database user account names need not (and perhaps should not) represent actual user accounts on the system.

With that completed you've successfully configured MySQL and you may now pass these database credentials on to your users. To exit the MySQL database administration utility issue the following command:

quit

With Apache and MySQL installed you are now ready to move on to installing PHP to provide scripting support for your web pages.

4. Install and Configure PHP


Issue the following command to install PHP:

apt-get install php5 php-pear

Once PHP5 is installed we'll need to tune the configuration file located in /etc/php5/apache2/php.ini to enable more descriptive errors, logging, and better performance. These modifications provide a good starting point if you're unfamiliar with PHP configuration.
Make sure that the following values are set, and relevant lines are uncommented (comments are lines beginning with a semi-colon (;)):

File excerpt:/etc/php5/apache2/php.ini

max_execution_time = 30
memory_limit = 64M
error_reporting = E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ERROR|E_CORE_ERROR
display_errors = Off
log_errors = On
error_log = /var/log/php.log
register_globals = Off

After making changes to the PHP configuration file, restart Apache by issuing the following command:

service apache2 reload

If you need support for MySQL in PHP, then you must install the php5-mysql package with the following command:

apt-get install php5-mysql

To install the php5-suhosin package, which provides additional security for PHP 5 applications (recommended), issue the following command:

apt-get install php5-suhosin

Restart Apache to make sure everything is loaded correctly:

service apache2 reload

- Test PHP

In the terminal copy/paste the following line:

vi /var/www/info.php

This will open up a file called info.php

Copy/Paste this line into the phptest file:

<?php phpinfo(); ?>

Save and close the file.

Now open you're web browser and type the following into the web address:

http://localhost/info.php

The page should show all the info regarding the php installed in your system:

Congratulations! You have now set up and configured a LAMP system.

Thanks to Linode Library for the procedure!