LAMP Server on Ubuntu 12.04 (Precise Pangolin)
Updated by Alex Fornuto
Setting up a LAMP (Linux, Apache, MySql, PHP) stack will allow for the creation and hosting of websites and web applications. This guide provides step by step instructions for installing a LAMP stack on an Ubuntu 12.04 (Precise Pangolin) Linode.
This guide is written for a non-root user. Commands that require elevated privileges are prefixed with
sudo. If you’re not familiar with the
sudocommand, you can check our Users and Groups guide.
Before You Begin
Update your system:
sudo apt-get update && sudo apt-get upgrade
Install and Configure
Install Apache 2:
sudo apt-get install apache2
Edit the main Apache configuration file to adjust the resource use settings. The settings shown below are a good starting point for a Linode 1GB:
1 2 3 4 5 6 7 8 9
KeepAlive Off <IfModule mpm_prefork_module> StartServers 2 MinSpareServers 6 MaxSpareServers 12 MaxClients 30 MaxRequestsPerChild 3000 </IfModule>
Configure Name-based Virtual Hosts
There are different ways to set up virtual hosts; however, the method below is recommended.
/etc/apache2/sites-available/directory, create a configuration file for your website,
example.comwith your own domain information:
1 2 3 4 5 6 7 8
<VirtualHost *:80> ServerAdmin email@example.com ServerName example.com ServerAlias www.example.com DocumentRoot /var/www/example.com/public_html/ ErrorLog /var/www/example.com/logs/error.log CustomLog /var/www/example.com/logs/access.log combined </VirtualHost>
CustomLogentries are suggested for more fine-grained logging, but are not required. If they are defined (as shown above), the
logsdirectories must be created before you restart Apache.
Create the directories referenced above:
sudo mkdir -p /var/www/example.com/public_html sudo mkdir /var/www/example.com/logs
Enable the website’s virtual host:
sudo a2ensite example.com.conf
If you need to disable your website later, run:
sudo a2dissite example.com.conf
sudo service apache2 reload
Assuming that you have configured the DNS for your domain to point to your Linode’s IP address, virtual hosting for your domain should now work.
Install and Configure
sudo apt-get install mysql-server
Choose a secure password when prompted.
mysql_secure_installation, a program that helps secure MySQL. 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:
Create a MySQL Database
Log into MySQL:
mysql -u root -p
Enter the root password. The MySQL prompt will appear.
Create a database and a user with permissions for it. In this example the database is called
webdata, the user
create database webdata; grant all on webdata.* to 'webuser' identified by 'password';
With Apache and MySQL installed you are now ready to install PHP.
Install PHP, and the PHP Extension and Application Repository:
sudo apt-get install php5 php-pear
If you need MySQL support, also install
sudo apt-get install php5-mysql
Once PHP5 is installed, tune the configuration file located in
/etc/php5/apache2/php.inito enable more descriptive errors, logging, and better performance. The following modifications provide a good starting point:
1 2 3
error_reporting = E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ERROR|E_CORE_ERROR error_log = /var/log/php/error.log max_input_time = 30
Ensure the lines above are uncommented. Commented lines begin with a semicolon (;).
Create the log directory for PHP and give the Apache user ownership:
sudo mkdir /var/log/php sudo chown www-data /var/log/php
sudo service apache2 restart
Congratulations! You have now set up and configured a LAMP stack.
You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.
- Ubuntu Server Edition Homepage
- Apache HTTP Server Documentation
- MySQL Documentation
- PHP Documentation
This guide is published under a CC BY-ND 3.0 license.