You are on page 1of 8

Install LAMP on Ubuntu 16.

04
Before You Begin
1. Ensure that you have followed the Getting Started and Securing Your Server guides and that the Linodes hostname is set.
2. Update your system:
1 sudo apt-get update && sudo apt-get upgrade

Apache
Install and Configure
1. Install Apache 2.4 from the Ubuntu repository:
1 sudo apt-get install apache2

2. Edit the main Apache configuration file, apache2.conf , to adjust the KeepAlive setting:
/etc/apache2/apache2.conf
1 KeepAlive Off

3. The default multi-processing module (MPM) for Apache is the event module but by default PHP uses the prefork module.
Open the mpm_prefork.conf file located in /etc/apache2/mods-available and edit the configuration. Below are the suggested
values for a 2GB Linode:
/etc/apache2/mods-available/mpm_prefork.conf

1 <IfModule mpm_prefork_module>
2
StartServers
3
MinSpareServers
4
MaxSpareServers
5
MaxRequestWorkers
6
MaxConnectionsPerChild
7 </IfModule>

4
20
40
200
4500

4. Disable the event module and enable prefork:


1 sudo a2dismod mpm_event
2 sudo a2enmod mpm_prefork

5. Restart Apache:
1 sudo systemctl restart apache2

Configure Virtual Hosts


You can set up virtual hosts several ways; however, below is the recommended method. By default, Apache listens on all IP
addresses available to it. For all steps below, replace example.com with your domain name.
1. Create a copy of the default Apache configuration file for your site:
1 sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/example.com.conf

2. Edit the new example.com.conf configuration file by uncommenting ServerName and replacing example.com with your sites IP or
Fully Qualified Domain Name (FQDN). Enter the document root path and log directories as shown below, and add a
Directory block before </VirtualHost> :
/etc/apache2/sites-available/example.com.conf
1 <Directory /var/www/html/example.com/public_html>
2
Require all granted
3 </Directory>
4 <VirtualHost *:80>
5
ServerName example.com
6
ServerAlias www.example.com
7
ServerAdmin webmaster@localhost
8
DocumentRoot /var/www/html/example.com/public_html
9
10
ErrorLog /var/www/html/example.com/logs/error.log
11
CustomLog /var/www/html/example.com/logs/access.log combined
12
13 </VirtualHost>

The file example above has all comment sections removed for brevity; you may keep or remove the
commented areas as you see fit.
The ServerAlias directive allows you to include multiple domain names or subdomains for a single host. The
example above allows visitors to use example.com or www.example.com to navigate to this virtual host.
3. Create the directories referenced above:
1 sudo mkdir -p /var/www/html/example.com/{public_html,logs}

4. Link your virtual host file from the sites-available directory to the sites-enabled directory:

1 sudo a2ensite example.com.conf

5. If you need to disable your website, run:

1 a2dissite example.com.conf

6. Disable the default virtual host to minimize security risks:


1 sudo a2dissite 000-default.conf

7. Reload Apache:
1 sudo systemctl reload apache2

8. Virtual hosting should now be enabled. To allow the virtual host to use your domain name, be sure that you have
configured DNS services for your domain to point to your Linodes IP address.
9. If there are additional websites you wish to host on your Linode, repeat the above steps to add a folder and configuration
file for each.

MySQL
Install and Configure
1. Install the mysql-server package:

1 sudo apt-get install mysql-server

2. Choose a secure password when prompted.


3. Run 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:
1 mysql_secure_installation

Create a MySQL Database


1. Log into MySQL:
1 mysql -u root -p

2. Enter MySQLs root password, and youll be presented with a MySQL prompt.
3. Create a database and a user with permissions for it. In this example, the database is called webdata , the user webuser , and
password password :
1 CREATE DATABASE webdata;
2 GRANT ALL ON webdata.* TO 'webuser' IDENTIFIED BY 'password';

4. Exit MySQL:

1 quit

PHP 7.0
1. Install PHP, the PHP Extension and Application Repository, Apache support, and MySQL support:
1 sudo apt-get install php7.0 php-pear libapache2-mod-php7.0 php7.0-mysql

2. Optionally, install additional cURL, JSON, and CGI support:


1 sudo apt-get install php7.0-curl php7.0-json php7.0-cgi

3. Once PHP7.0 is installed, edit the configuration file located in /etc/php/7.0/apache2/php.ini to enable more descriptive
errors, logging, and better performance. The following modifications provide a good starting point:
/etc/php/7.0/apache2/php.ini
1 max_input_time = 30
2 error_reporting = E_COMPILE_ERROR | E_RECOVERABLE_ERROR | E_ERROR | E_CORE_ERROR
3 error_log = /var/log/php/error.log

The beginning of the php.ini file contains examples commented out with a semicolon (;), which disables
these directives. Ensure that the lines you modify in this step are after the examples section and are
uncommented.

4. Create the log directory for PHP and give ownership to the Apache system user:
1 sudo mkdir /var/log/php
2 sudo chown www-data /var/log/php

5. Restart Apache:
1 sudo systemctl restart apache2

Optional: Test and Troubleshoot the LAMP Stack


In this section, well create a test page that shows whether Apache can render PHP and connect to the MySQL database. This
can be helpful in locating the source of an error if one of the elements of your LAMP stack is not communicating with the others.
1. Paste the following code into a new file, phptest.php , in the public_html directory. Modify webuser and password to match the
information entered in the Create a MySQL Database section above:
/var/www/html/example.com/public_html/phptest.php
1 <html>
2 <head>
3
<title>PHP Test</title>
4 </head>
5
<body>
6
<?php echo '<p>Hello World</p>';
7
8
// In the variables section below, replace user and password with your own MySQL credentials as created on
9 your server
10
$servername = "localhost";
11
$username = "webuser";
12
$password = "password";

13
14
// Create MySQL connection
15
$conn = mysqli_connect($servername, $username, $password);
16
17
// Check connection - if it fails, output will include the error message
18
if (!$conn) {
19
die('<p>Connection failed: <p>' . mysqli_connect_error());
20
}
21
echo '<p>Connected successfully</p>';
22
?>
23 </body>
</html>

2. Navigate to example.com/phptest.php from your local machine. If the components of your LAMP stack are working correctly,
the browser will display a Connected successfully message. If not, the output will be an error message.

Troubleshooting

If the site does not load at all, check if Apache is running, and restart it if required:
1 systemctl status apache2
2 sudo systemctl restart apache2

If the site loads, but the page returned is the default Congratulations page, return to the Configure Virtual Hosts section
above, and check that the DocumentRoot matches your example.com/public_html folder.
If the page returned says Index of / or has a similar folder tree structure, create a test index.html file or a test file as
shown above.
Congratulations! You have now set up and configured a LAMP stack on Ubuntu 16.04 (LTS).