LAMP Stack,LAMP Stack on Ubuntu (Linux, Apache, MySQL, PHP) stack is a not unusual, unfastened and open-supply web stack used for website hosting web content in a Linux environment. Many recollect it the platform of preference on which to expand and deploy high-performance internet apps.
This manual shows how to set up and check a LAMP stack on Ubuntu sixteen.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, see the Linux Users and Groups guide.
Replace each instance of example.com in this guide with your site’s domain name.
LAMP Stack,LAMP Stack on Ubuntu Before You Begin
Ensure that you have followed the Getting Started and Securing Your Server guides and that the Linode’s hostname is set.
Update your system:
sudo apt update && sudo apt upgrade
LAMP Stack,LAMP Stack on Ubuntu Quick Install Using Tasksel
LAMP Stack,Instead of installing Apache, MySQL, and PHP separately, tasksel offers a convenient way to get a LAMP stack running quickly.
- Install tasksel if not already installed by default.
sudo apt install tasksel
- Use tasksel to install the LAMP stack.
sudo tasksel install lamp-server
- Enter the prompt for a MySQL root password. See the steps below for Apache configurations, creating a virtual host, and installation of PHP modules for WordPress installation.
Apache
Install and Configure Apache
- Install Apache 2.4 from the Ubuntu repository:
sudo apt install apache2
- The
KeepAlive
setting allows Apache to 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.The state ofKeepAlive
depends on the type of site you plan to run. Please read more about your specific use-case here open the Apache config file,apache2.conf
, and adjust theKeepAlive
setting:
/etc/apache2/apache2.conf
KeepAlive On
MaxKeepAliveRequests 50
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 controls how long the server waits for new requests from already connected clients, setting this option to 5 will avoid wasting RAM.
03. The default multi-processing module (MPM) is the prefork module. Mpm_prefork
is the module that is compatible with most systems. Since the LAMP stack requires PHP, it may be best to stick with the default. 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
<IfModule mpm_prefork_module>
1. StartServers 4
2. MinSpareServers 3
3. MaxSpareServers 40
4. MaxRequestWorkers 200
5. MaxConnectionsPerChild 10000
</IfModule>
04. Disable the event module and enable prefork:
sudo a2dismod mpm_event
sudo a2enmod mpm_preforkRestart Apache:
05.Restart Apache:
sudo systemctl restart apache2
Configure Virtual Hosts
You can installation virtual hosts several methods; but, below is the recommended method. By default, Apache listens on all IP addresses available to it. For all steps underneath, update instance.Com with your area name.
01. Create a copy of the default Apache configuration file for your site:
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/example.com.conf
02. Edit the new example.com.conf
configuration file by uncommenting ServerName
and replacing example.com
with your site’s 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>
:
<Directory /var/www/html/example.com/public_html>
Require all granted
</Directory>
<VirtualHost *:80>
1.ServerName example.com
2.ServerAlias www.example.com
3.ServerAdmin webmaster@localhost
4.DocumentRoot /var/www/html/example.com/public_html
5.ErrorLog /var/www/html/example.com/logs/error.log
6.CustomLog /var/www/html/example.com/logs/access.log combined
</VirtualHost>
Note The report example above has all comment sections removed for brevity; you could hold or cast off the commented regions as you notice healthy.
The ServerAlias directive allows you to include a couple of domains or subdomains for a unmarried host. The instance above lets in visitors to apply example.Com or www.Example.Com to navigate to this digital host.
03. Create the directories referenced above:
sudo mkdir -p /var/www/html/example.com/{public_html,logs}
Note Make sure that you do not put space after 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.
04. Link your virtual host file from the sites-available
directory to the sites-enabled
directory:
sudo a2ensite example.com.conf
Note
If you need to disable your website, run:
a2dissite example.com.conf
5. Disable the default virtual host to minimize security risks:
sudo a2dissite 000-default.conf
06. Reload Apache:
sudo systemctl reload apache2
Virtual hosting must now be enabled. To allow the digital host to use your domain call, make sure that you have configured DNS services for your domain to point in your Linode’s IP cope with.
If there are additional websites you desire to host on your Linode, repeat the above steps to feature a folder and configuration file for every.
Install MySQL
Install the mysql-server
package and choose a secure password when prompted:
sudo apt install mysql-server
Create a MySQL Database
Log into MySQL:
mysql -u root -p
- Enter MySQL’s root password, and you’ll be presented with a MySQL prompt.
- Replace
password
with a new root password:
ALTER USER 'root'@'localhost' IDENTIFIED WITH 'mysql_native_password' BY 'password';
03.Create a database and a user with permissions for it. In this example, the database is called webdata
, the user webuser
, and password password
:
CREATE DATABASE webdata;
GRANT ALL ON webdata.* TO 'webuser' IDENTIFIED BY 'password';
04. Exit MySQL:
quit
PHP 7.0
- Install PHP, the PHP Extension and Application Repository, Apache support, and MySQL support:
sudo apt install php7.0 libapache2-mod-php7.0 php7.0-mysql
Optionally, install additional cURL, JSON, and CGI support:sudo apt install php7.0-curl php7.0-json php7.0-cgi
- 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.1 2 3 max_input_time = 30 error_reporting = E_COMPILE_ERROR | E_RECOVERABLE_ERROR | E_ERROR | E_CORE_ERROR error_log = /var/log/php/error.log
NoteThe beginning of thephp.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. - Create the log directory for PHP and give ownership to the Apache system user:
sudo mkdir /var/log/php sudo chown www-data /var/log/php
- Restart Apache:
sudo systemctl restart apache2
NoteIf you plan on using your LAMP stack to host a WordPress server, download these PHP modules:apt install php-curl php-gd php-mbstring php-mcrypt 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.
- Paste the following code into a new file,
phptest.php
, in thepublic_html
directory. Modifywebuser
andpassword
to match the information entered in the Create a MySQL Databasesection above:/var/www/html/example.com/public_html/phptest.php1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
<html> <head> <title>PHP Test</title> </head> <body> <?php echo '<p>Hello World</p>'; // In the variables section below, replace user and password with your own MySQL credentials as created on your server $servername = "localhost"; $username = "webuser"; $password = "password"; // Create MySQL connection $conn = mysqli_connect($servername, $username, $password); // Check connection - if it fails, output will include the error message if (!$conn) { die('<p>Connection failed: <p>' . mysqli_connect_error()); } echo '<p>Connected successfully</p>'; ?> </body> </html>
- 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:
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 yourexample.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).