Sei sulla pagina 1di 9

How to Install a LAMP Stack on Ubuntu 18.

04
Updated Monday, June 4, 2018 by Edward AngertWritten by Linode

What is a LAMP Stack?


A LAMP (Linux, Apache, MySQL, PHP) stack is a common, free, and open-source web stack used for hosting web content in a Linux
environment. Many consider it the platform of choice on which to develop and deploy high-performance web apps.

This guide shows how to install and test a LAMP stack on Ubuntu 18.04 (LTS).

Note
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 sudo command, visit our Users and Groups guide.
All configuration files should be edited with elevated privileges. Remember to include sudo before running your text editor.
Replace each instance of example.com in this guide with your site’s domain name or IP.

Before You Begin


1. Ensure that you have followed the Getting Started and Securing Your Server guides and that the Linode’s hostname is set.
2. Update your system:

3. sudo apt update && sudo apt upgrade

Installation
Install Using Tasksel
Instead of installing Apache, MySQL, and PHP separately, Tasksel offers a convenient way to get a LAMP stack running quickly.

1. Install Tasksel if not already installed by default:

1
2. sudo apt install tasksel

3. Use Tasksel to install the LAMP stack:

4. sudo tasksel install lamp-server

Install Packages Separately


If you prefer not to install the bundled packages via Tasksel, you can instead install them separately:

1. Install Apache 2.4 from the Ubuntu repository:

2. sudo apt install apache2

3. Install the mysql-server package:


4. sudo apt install mysql-server

5. Install PHP, the PHP Extension and Application Repository, Apache support, and MySQL support:

6. sudo apt install php7.2 libapache2-mod-php7.2 php-mysql

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

sudo apt install php-curl php-json php-cgi

Configuration
Apache
1. The KeepAlive setting allows Apache to better utilize server-side memory, reducing latency for users on the hosted
site. KeepAlive will make a website faster if the host has enough memory to support it. This is done by allowing Apache to reuse
connections, instead of opening a new connection for every request.
Open the apache2.conf Apache config file and adjust the KeepAlive setting:
2
/etc/apache2/apache2.conf
1 KeepAlive On
2 MaxKeepAliveRequests 50
3 KeepAliveTimeout 5

Note

The MaxKeepAliveRequests setting controls the maximum number of requests during a persistent connection. 50 is a conservative
amount; you may need to set this number higher depending on your use-case. The KeepAliveTimeout setting controls how long the
server waits (measured in seconds) for new requests from already connected clients. Setting this to 5 will avoid wasting RAM.

2. The default multi-processing module (MPM) is the prefork module. mpm_prefork is the module that is compatible with most
systems. 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 4
3 MinSpareServers 3
4 MaxSpareServers 40
5 MaxRequestWorkers 200
6 MaxConnectionsPerChild 10000
7 </IfModule>

3. Disable the event module and enable prefork:

4. sudo a2dismod mpm_event


5. sudo a2enmod mpm_prefork

6. Restart Apache:

7. sudo systemctl restart apache2

3
Virtual Hosts
You can set up virtual hosts several ways, and the following steps outline the recommended method. For each of these steps,
replace example.com with your domain name.
1. Create a copy of the default Apache configuration file for your site:

2. sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/example.com.conf

3. Open the new example.com configuration file in your text editor. Uncomment the ServerNameoption and update it with your domain.
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>

Note

The file example above has all comment sections removed for brevity. 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.
4. Create the directories referenced above:
4
5. sudo mkdir -p /var/www/html/example.com/{public_html,logs}

Note

Make sure that you do not put a space after the comma between public_html and logs because it will create a folder
named {public_html, and will cause an error when you will reload Apache.

6. Link your virtual host file from the sites-available directory to the sites-enabled directory:
7. sudo a2ensite example.com

Note

To disable your website, run a2dissite example.com.

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

9. sudo a2dissite 000-default.conf

10. Reload Apache:

11. sudo systemctl reload apache2

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 Linode’s IP address.
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
1. Log in to MySQL’s SQL shell:

2. sudo mysql -u root

About MySQL authentication


5
By default, MySQL is configured to use the auth_socket authorization plugin. This authorization scheme allows you to log in to the
database’s root user as long as you are connecting from the Linux root user on localhost, or as a user with sudo privileges (i.e.
with sudo mysql -u root). In this scheme, no password is assigned to MySQL’s root user:
mysql> SELECT user,host,authentication_string,plugin FROM mysql.user WHERE user='root';
+------+-----------+-----------------------+-------------+
| user | host | authentication_string | plugin |
+------+-----------+-----------------------+-------------+
| root | localhost | | auth_socket |
+------+-----------+-----------------------+-------------+
1 row in set (0.02 sec)

You can keep using the auth_socket plugin, and this is considered a secure option for production systems. If you’d rather switch to
password authentication and assign a password, enter the following commands. Replace password with a new root password:
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH 'mysql_native_password' BY 'password';
mysql> FLUSH PRIVILEGES;

After making this change, you should pass the -p option when invoking the MySQL shell:
mysql -u root -p

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. Be sure to enter your own password. This should be different from the root password for MySQL:
4. CREATE DATABASE webdata;

GRANT ALL ON webdata.* TO 'webuser' IDENTIFIED BY 'password';

5. Exit the SQL shell:

quit

6. Use the mysql_secure_installation tool to configure additional security options. This tool will ask if you want to set a new password
for the MySQL root user, but you can skip that step:
6
7. sudo mysql_secure_installation

Answer Y at the following prompts:


o Remove anonymous users?
o Disallow root login remotely?
o Remove test database and access to it?
o Reload privilege tables now?

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

Note

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 follow the examples section and are uncommented.

2. Create the log directory for PHP and give ownership to the Apache system user:

3. sudo mkdir /var/log/php


4. sudo chown www-data /var/log/php

5. Restart Apache:

6. sudo systemctl restart apache2

Note
7
If you plan on using your LAMP stack to host a WordPress server, install additional PHP modules: sudo apt install php-curl
php-gd php-mbstring php-xml php-xmlrpc

Optional: Test and Troubleshoot the LAMP Stack


In this section, you’ll 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 Databasesection 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 c
9 reated on 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>
24 </html>

8
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.
3. Remove the test file:

4. sudo rm /var/www/html/example.com/public_html/phptest.php

Troubleshooting
 If the site does not load at all, check if Apache is running, and restart it if required:

 sudo systemctl status apache2


 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 18.04 (LTS).

More Information
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

Potrebbero piacerti anche