In this article we will be configuring basic LAMP server on Ubuntu 20.04 Focal Fossa.

In this tutorial you will learn:
  • How to install LAMP server
  • How to open firwall port to allow HTTP and HTTPS incoming traffic
  • How to connect to MySQL database using PHP script.
LAMP server setup on Ubuntu 20.04 Focal Fossa
LAMP server setup on Ubuntu 20.04 Focal Fossa

Software Requirements and Conventions Used

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Installed 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

How to setup LAMP server on Ubuntu 20.04 step by step instructions

  1. You can install the LAMP server stack either by using the apt command and inlcuding 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
    

  2. SUBSCRIBE TO NEWSLETTER
    Subscribe to Linux Career NEWSLETTER and receive latest Linux news, jobs, career advice and tutorials.


  3. Enable the Mysql/MariaDB and Apache to start after the reboot:
    $ sudo systemctl enable --now mysql
    $ sudo systemctl enable --now apache2
    
  4. Configure MySQL/MariaDB database. First, perform the secure installation:
    $ sudo mysql_secure_installation
    
    Next, let us test connection to the MySQL database programmaticly using PHP script. Just for the testing purposes we will be using LOW password policy. Connect to MySQL from the command line:
    root@linuxconfig:~# mysql -u root -p
    
    In the next steps we will create sample database and user. First, confirm your password policy and then create new user admin and grant the user full privileges to the new linuxconfig 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:
    Using PHP script to connect to MySQL database on Ubuntu 20.04
    Using PHP script to connect to MySQL database on Ubuntu 20.04


  5. Optionally enable the HTTPS to provide secure connection the 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.
  6. 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 your 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 he 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 LOW password policy:
mysql> SET GLOBAL validate_password.policy = LOW;
ARE YOU LOOKING FOR A LINUX JOB?
Submit your RESUME, create a JOB ALERT or subscribe to RSS feed on LinuxCareers.com.
LINUX CAREER NEWSLETTER
Subscribe to NEWSLETTER and receive latest news, jobs, career advice and tutorials.
DO YOU NEED ADDITIONAL HELP?
Get extra help by visiting our LINUX FORUM or simply use comments below.