Ubuntu 22.04 LAMP installation

A LAMP stack is an assortment of software that contains everything you need in order to serve a website, show dynamic content, and store or retrieve data from a database. The software is all within the LAMP acronym, namely the Linux operating system, Apache web server, MySQL database (or MariaDB alternatively), and PHP programming language.

The components that LAMP is built from are all open source. If you’ve downloaded Ubuntu 22.04 and also installed it on your PC, then you already have the first requirement done. Next, you just need to get your LAMP stack up and running. In this guide, we’ll show the step by step instructions to install a LAMP stack on Ubuntu 22.04 Jammy Jellyfish Linux.

In this tutorial you will learn:

  • How to install LAMP server on Ubuntu 22.04
  • How to open firewall port to allow HTTP and HTTPS incoming traffic
  • How to connect to MySQL database using a PHP script
LAMP server setup on Ubuntu 22.04 Jammy Jellyfish
LAMP server setup on Ubuntu 22.04 Jammy Jellyfish
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Ubuntu 22.04 Jammy Jellyfish
Software LAMP
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 22.04 step by step instructions



Twitter icon Follow LinuxConfig.org on Twitter for the latest tips and tricks about Linux!

  1. You can install the LAMP server stack either by using the apt command and including the bare minimum number of packages:
    $ sudo apt update
    $ sudo apt install apache2 php-mysql libapache2-mod-php mysql-server
    

    or by using the tasksel command:

    $ sudo tasksel install lamp-server
    
  2. If you want your web server stack to start up automatically upon system reboots, you will need to enable MySQL and Apache in systemd with the following commands. Doing this will also start the services right now since we supply the --now option.
    $ sudo systemctl enable --now mysql
    $ sudo systemctl enable --now apache2
    
  3. Execute the following command to secure your MySQL installation and set the root password. There will be a few prompts to go through. It’s recommended that you disable remote root login when the question shows up, unless you have some circumstance that requires it.
    $ sudo mysql_secure_installation
    
  4. To confirm that our web server is accessible and that PHP is working as expected, we can create a file called info.php inside the /var/www/html directory. The file should contain the following line, which verifies whether PHP is working or not.
    
    



  5. In your browser, navigate to the test page we’ve created by opening the URL at http://localhost/info.php. You should see a result like the one in the screenshot below.

    The PHP info page indicates a successful installation of Apache and PHP
    The PHP info page indicates a successful installation of Apache and PHP
  6. Next, let’s configure a user and database for our MySQL server.
    $ sudo mysql
    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;
    mysql> exit
    

    With these commands, we have created a new user named admin, which has full permissions on a database called linuxconfig.

  7. With our MySQL database and user configured, we can create a simple test page to see if we’re able to connect to MySQL via php. Paste the following script in a file, which we will then access from a web browser. Be sure to substitute the database name, username, and password with those of your own. We will store this file as /var/www/html/mysql-test.php
    <?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(); ?>
    




    Then, try to access the page:

    Our MySQL test page indicates that PHP is able to communicate with the MySQL database
    Our MySQL test page indicates that PHP is able to communicate with the MySQL database
  8. Optionally, you can setup SSL encryption with a free certificate from Let’s Encrypt. Simply install the certificate bot and run through the prompts to answer some questions. Of course, you will need a fully qualified domain name pointed at your web server for this to work.
    $ sudo apt install certbot python3-certbot-apache
    $ certbot --apache
    
  9. Lastly, open firewall port 80 and 443 to allow remote incoming traffic:
    $ sudo ufw allow in "Apache Full"
    

Closing Thoughts

In this tutorial, we saw how to install and configure a Ubuntu 22.04 LAMP web server stack. This involved installing the individual software packages, namely Apache, MariaDB, and PHP. LAMP is an excellent way to get a full stack up and running in very short time. Your system is now ready to serve and store dynamic web content.