LAMP is a conventional model of web service stacks. The components that LAMP is built from are all open-source and include: the Linux operating system, the Apache HTTP Server, the MySQL relational database management system, and the PHP programming language. In this short tutorial we will be configuring a basic LAMP server on Ubuntu 20.04 Focal Fossa.
You may also be interested in our article on creating a docker based LAMP stack on Ubuntu 20.04.
In this tutorial you will learn:
- How to install LAMP server on Ubuntu 20.04.
- How to open firewall port to allow HTTP and HTTPS incoming traffic.
- How to connect to MySQL database using a PHP script.
Software Requirements and Conventions Used
Category | Requirements, Conventions or Software Version Used |
---|---|
System | Installed Ubuntu 20.04 or upgraded Ubuntu 20.04 Focal Fossa |
Software | N/A |
Other | Privileged access to your Linux system as root or via the sudo command. |
Conventions |
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command$ – requires given linux commands to be executed as a regular non-privileged user |
Setting up LAMP server on Ubuntu 20.04 step by step instructions
- You can install the LAMP server stack either by using the
apt
command and including the bare minimum number of packages:$ sudo apt install php-mysql libapache2-mod-php mysql-server
or by using the
tasksel
command:$ sudo tasksel install lamp-server
- Enable the Mysql/MariaDB and Apache to start after the reboot:
$ sudo systemctl enable --now mysql $ sudo systemctl enable --now apache2
- Configure MySQL/MariaDB database. First, perform the secure installation:
$ sudo mysql_secure_installation
Next, let us test connection to the MySQL database programmatically using a PHP script. Just for the testing purposes we will be using a
LOW
password policy. Connect to MySQL from the command line:root@linuxconfig:~# mysql -u root -p
In the next steps we will create a sample database and a user. First, confirm your password policy and then create new user
admin
and grant the user full privileges to the newlinuxconfig
database:mysql> SHOW VARIABLES LIKE 'validate_password%'; +--------------------------------------+-------+ | Variable_name | Value | +--------------------------------------+-------+ | validate_password.check_user_name | ON | | validate_password.dictionary_file | | | validate_password.length | 8 | | validate_password.mixed_case_count | 1 | | validate_password.number_count | 1 | | validate_password.policy | LOW | | validate_password.special_char_count | 1 | +--------------------------------------+-------+ 7 rows in set (0.01 sec) mysql> CREATE DATABASE linuxconfig; mysql> CREATE USER `admin`@`localhost` IDENTIFIED WITH mysql_native_password BY 'yourpass'; mysql> GRANT ALL ON linuxconfig.* TO `admin`@`localhost`; mysql> FLUSH PRIVILEGES;
Next, create the following PHP script called eg.
/var/www/html/php-mysql-connect.php
to connect to your local MySQL database:<?php $conn = new mysqli("localhost", "admin", "yourpass", "linuxconfig"); if ($conn->connect_error) { die("ERROR: Unable to connect: " . $conn->connect_error); } echo 'Connected to the database.
'; $conn->close(); ?>Make the script executable:
$ sudo chmod +x /var/www/html/php-mysql-connect.php
Once ready, open and navigate your browser to
http://localhost/php-mysql-connect.php
: - Optionally enable the
HTTPS
to provide secure connection to our Apache webserver. Please note that we will be using the default Apache2 settings with a default self-signed SSL certificates:Default SSL certificates
Please note that we are using the default SSL certificates. You care recommended to upload your SSL certificates or use Let’s Encrypt to generate new certificates for your domain(s).$ sudo a2ensite default-ssl $ sudo a2enmod ssl $ sudo systemctl restart apache2
Next, navigate to
https://localhost/
using your browser. - Lastly, open firewall port 80 and 443 to allow remote incoming traffic:
$ sudo ufw allow in "Apache Full"
You can now create a following script
/var/www/html/phpinfo.php
with the below content to see your LAMP configuration settings and enabled modules:<?php phpinfo(); ?>
Do not forget to make the PHP script executable:
chmod +x /var/www/html/phpinfo.php
Use the following URL to connect to your
phpinfo.php
script:http://YOURSERVER-OR-IP/phpinfo.php
.
Troubleshooting
The server requested authentication method unknown to the client
This error means that you are unable to authenticate your user by using a password. This method needs to be specifically enabled. Try to update your MySQL user settings be execution of the below command and editing the username and password on the below MySQL command to fit your environment:
mysql> ALTER user 'username'@'localhost' identified with mysql_native_password by 'password';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
Review your password policy and make sure the given password complies with the requirements:
mysql> SHOW VARIABLES LIKE 'validate_password%'; +--------------------------------------+--------+ | Variable_name | Value | +--------------------------------------+--------+ | validate_password.check_user_name | ON | | validate_password.dictionary_file | | | validate_password.length | 8 | | validate_password.mixed_case_count | 1 | | validate_password.number_count | 1 | | validate_password.policy | MEDIUM | | validate_password.special_char_count | 1 | +--------------------------------------+--------+
Alternatively, switch to another password policy. For example the below command will switch to the LOW
password policy:
mysql> SET GLOBAL validate_password.policy = LOW;