How to install Linux, Apache, MySQL, and PHP (LAMP) on Ubuntu 20

2021-07-01 By Nathan 60 Views linux ubuntu lamp apache mysql mariadb php development
0 reviews

The LAMP stack stands for Linux, Apache, MySQL/MariaDB and PHP - a simple and stable group of 4 basic open-source software that are needed for building dynamic websites and applications.


By default, these software are not installed in an Ubuntu 20 installation. This guide outlines the basic steps to set up LAMP on Ubuntu 20 installed Cloud Servers.


Installing Apache Webserver


  1. Log in to the Ubuntu server through SSH and then update the package information index by executing the below command.

    # sudo apt update
    

    ubuntu20lamp1

  2. Upgrade the server packages using the below command.

    # sudo apt -y upgrade
    

    ubuntu20lamp2

  3. Install the Apache web server by running the below command.

    # sudo apt install -y apache2
    

    ubuntu20lamp3

  4. Once the installation of Apache web server has been completed, the working of the service can be checked and verified by browsing the public IP address of the Cloud Servers through a browser.

    http://123.123.123.123
    

    NOTE: Replace 123.123.123.123 with the actual IP address of the server.


    If the service has been installed successfully, a default Ubuntu Apache web page will be displayed as below:

    ubuntu20lamp4


    Once the Apache web server has been installed, the next step in LAMP setup involves installing the database (MySQL/MariaDB) server.


Installing Database Server


MySQL and MariaDB are both database servers that can be used in a LAMP stack and either one of them can be installed in the server as per requirement.


  1. For installing MySQL database server, execute the below command through SSH command line:

    # sudo apt install -y mysql-server
    

    ubuntu20lamp5


    For installing MariaDB database server, the below command can be run:

    # sudo apt install -y mariadb-server mariadb-client
    

    ubuntu20lamp6

  2. Run the below command to secure the database server. This command is independent of the type of database server that has been installed.

    # sudo mysql_secure_installation
    


    Complete the security setup as below:


    For MySQL:

    ubuntu20lamp7

    NOTE: Replace with the password that needs to be set as the MySQL root password.

    For MariaDB:

    ubuntu20lamp8

    NOTE: Replace with the password that needs to be set as the MariaDB root password.

  3. Once the database server has been secured, the database server can be accessed by executing the below command.

    # sudo mysql -u root -p
    


    Type in the root password set for the database server in above step and then press Enter to access the database server command line. Outputs for both MySQL and MariaDB will be as shown below:


    For MySQL:

    ubuntu20lamp9

    For MariaDB:

    ubuntu20lamp10

  4. Execute the below command in the database server command prompt to create a test database. The database name used in this context is layerstacktestdb.

    # CREATE database layerstacktestdb;
    


    Outputs are as shown below:


    For MySQL:

    ubuntu20lamp11

    For MariaDB:

    ubuntu20lamp12

  5. The newly created database as well as the existing databases can be viewed by executing the below command.

    # SHOW DATABASES;
    


    Output for above command will be as follows:


    For MySQL:

    ubuntu20lamp13

    For MariaDB:

    ubuntu20lamp14

  6. A test database user that can be associated with the already created test database layerstacktestdb can be created by executing the below set of commands depending upon the database server installed. The command assigns full privileges for the test database user to the database. These details can later be used to test database connectivity using PHP.


    The test database user used in this context is layerstacktestuser. Replace EXAMPLE_PASSWORD with the password that needs to be set for the database user.


    For MySQL:

    # CREATE USER 'layerstacktestuser'@'localhost' IDENTIFIED WITH mysql_native_password BY 'EXAMPLE_PASSWORD';
    
    # GRANT ALL PRIVILEGES ON layerstacktestdb.* TO ' layerstacktestuser'@'localhost';
    
    # FLUSH PRIVILEGES;
    
    # EXIT;
    

    ubuntu20lamp15

    For MariaDB:

    # CREATE USER 'layerstacktestuser'@'localhost' IDENTIFIED BY 'EXAMPLE_PASSWORD';
    
    # GRANT ALL PRIVILEGES ON layerstacktestdb.* TO 'layerstacktestuser'@'localhost';
    
    # FLUSH PRIVILEGES;
    
    # EXIT;
    

    ubuntu20lamp16


    Once the database server has been installed and secured, a test database and test database user created, the PHP scripting language can be installed in the server.


Installing PHP


  1. Run the below command to install PHP scripting language in server.

    # sudo apt install -y php
    

    ubuntu20lamp17

  2. Some of the most commonly used PHP modules can be installed using the below command. This command can be modified to install any additionally required PHP modules.

    # sudo apt install -y php-{common,mysql,xml,xmlrpc,curl,gd,imagick,cli,dev,imap,mbstring,opcache,soap,zip,intl}
    

    ubuntu20lamp18

  3. Once this has been completed, restart the Apache webserver to load the PHP service.

    # sudo systemctl restart apache2
    

    ubuntu20lamp19

  4. The installed PHP service can now be tested by placing a phpinfo page under the Apache default document root folder ( /var/www/html ).


    For this, first create the phpinfo page (info.php) with the help of any editor (such as vi or nano).

    # vi /var/www/html/info.php
    

    ubuntu20lamp20


    Type in the below content inside the info.php file, save the file and then exit the editor.

    <?php
    phpinfo();
    ?>
    
  5. Access the below URL in a browser to test and verify the working of PHP.

    http://123.123.123.123/info.php
    

    NOTE: Replace 123.123.123.123 with the actual IP address of the server.


    If PHP has been installed correctly, a detailed information page of PHP will be displayed as below:

    ubuntu20lamp21

  6. To complete the installation of LAMP stack, the last step would be to test the connectivity of the database that was created earlier with the newly installed PHP.


    For this, first create a new PHP file under the Apache default document root using any editor. The file name being used in this context is lsdb_connect.php.

    # vi /var/www/html/lsdb_connect.php
    

    ubuntu20lamp22


    Enter the below content inside the lsdb_connect.php file, save the file and then exit the editor.

    <?php
    $conn = new mysqli('localhost', 'layerstacktestuser', 'EXAMPLE_PASSWORD', 'layerstacktestdb');
    if ($conn->connect_error) {
    die("Database connection failed: " . $conn->connect_error);
    }
    echo "Database connection was successful";
    

    NOTE: Replace EXAMPLE_PASSWORD with the password that was set earlier for the database user.


    Access the below URL in a browser to test the database connectivity:

    http://123.123.123.123/lsdb_connect.php
    

    NOTE: Replace 123.123.123.123 with the actual IP address of the server.


    If the PHP script has connected to the database correctly, then the below page will be displayed:

    ubuntu20lamp23


    Once the installation of LAMP stack has been completed successfully in the server, websites and applications that run on LAMP can be setup in the Ubuntu 20 server.


Related Tutorials

What do you think about this article?

Rate this article