Skip to content

Instantly share code, notes, and snippets.

@reanim8ed
Last active July 20, 2023 16:23
Show Gist options
  • Save reanim8ed/eeb3aec22b8dde2066c8076ae80360d5 to your computer and use it in GitHub Desktop.
Save reanim8ed/eeb3aec22b8dde2066c8076ae80360d5 to your computer and use it in GitHub Desktop.
[LEMP in Ubuntu 20.04] #phpmyadmin #ubuntu #lemp

LEMP – Linux, Nginx (pronounced EngineX), MariaDB and PHP.

If you looking for a LAMP setup for your Ubuntu 20.04, then you should read our LAMP setup guide on Ubuntu 20.04.

Step 1: Installing Nginx on Ubuntu 20.04

  1. Nginx is a fast modern web server designed to server many concurrent connections without consuming too many server resources. This is why it’s often the preferred choice in enterprise environments.

NGINX is also commonly used as a load balancer and web content cache. It supports Name-based and IP-based virtual servers (analogous to virtual hosts in Apache).

You can install Nginx on your Ubuntu 20.04 desktop or server by running the following command.

$ sudo apt update
$ sudo apt install nginx

The Nginx configuration files are stored under the /etc/nginx directory and its main configuration file is /etc/nginx/nginx.conf. Importantly, its default document root for storing your web files is /usr/share/nginx/html/. But you can use the standard /var/www/html which should be configured in your website’s or application’s server block configuration file.

  1. The Ubuntu package installer triggers systemd to start the Nginx service and enable it to automatically start every time the server is rebooted. Use the following systemctl commands to confirm that the service is running and is enabled.
$ sudo systemctl status nginx 
$ sudo systemctl is-enabled nginx
  1. Now it’s time to check if the Nginx installation was successful by calling the Nginx page via browser using server IP Address. http://SERVER_IP If you don’t know your server IP address, you can find using IP command as shown. $ ip addr show

The NGINX default web page should load as shown in the following screenshot, confirming correct installation and operation.

Step 2: Installing MariaDB Database on Ubuntu 20.04

  1. MariaDB is a relatively new relational database management system that was designed as a community fork of MySQL after it’s Oracle acquisition.

The installation of MariaDB is simple and can be started with command as: $ sudo apt install mariadb-server mariadb-client

  1. The MariaDB service is also automatically started and enabled to always start at system boot and you can confirm this using the following commands.
$ sudo systemctl status mariadb
$ sudo systemctl is-enabled mariadb
  1. If you wish to improve the MariaDB security, you can run the mysql_secure_installation command, which will provide some basic, yet important options to configure: $ sudo mysql_secure_installation Then choose the option to set the database root (or administrator) user’s password and follow the prompts and carefully read the questions. To secure your database server, answer the questions:
Enter current password for root (enter for none): Enter
Set a root password? [Y/n] y
Remove anonymous users? [Y/n] y
Disallow root login remotely? [Y/n] y
Remove test database and access to it? [Y/n] y
Reload privilege tables now? [Y/n] y
  1. To create, manage, and perform database operations, you need to mysql shell command with the -u flag to specify the database username and -p to provide the user’s password. To connect as the root user, use the sudo command (even without the -p flag) otherwise you will receive the error highlighted in the following screenshot.
$ mysql -u root -p
$ sudo mysql -u root

Step 3: Installing PHP in Ubuntu 20.04

  1. PHP is a popular open-source, flexible, and dynamic scripting language for building websites and web applications. It supports a variety of programming techniques. Importantly, the PHP community is large and diverse, composed of countless libraries, frameworks, and other useful components.

$ sudo apt install php php-mysql php-fpm

Since PHP 7.4 is the default version of PHP in Ubuntu 20.04, the PHP configuration files are located in /etc/php/7.4/ and the PHP-FPM configuration files are stored under /etc/php/7.4/fpm.

  1. Next, check if the php7.4-fpm service is up and running and whether it is enabled with the following command.
$ sudo systemctl status php7.4-fpm
$ sudo systemctl is-enabled php7.4-fpm

Step 4: Configuring Nginx to work with PHP-FPM

  1. Now you need to configure NGINX to proxy client requests to PHP-FPM, which by default is configured to listen on a UNIX socket as defined by the listen parameter in the /etc/php/7.4/fpm/pool.d/www.conf default pool configuration file. $ sudo vi /etc/php/7.4/fpm/pool.d/www.conf

  2. In the default server block configuration file (/etc/nginx/sites-available/default), uncomment the location directive for processing PHP requests to look like the one shown in the following screenshot.

$ sudo vi /etc/nginx/sites-available/default

  1. Then test the NGINX configuration syntax for correctness. If it is OK, restart the Nginx service to apply the new changes.
$ sudo nginx -t
$ sudo systemctl restart nginx
  1. Now test if NGINX can work in conjunction with PHP-FPM to process PHP requests. Create a simple info.php page under the document root directory. $ echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php

  2. In your browser, navigate using the following address. The PHP configuration page should load showing as shown in the following screenshot. http://SERVER_IP/info.php

Step 5: Installing PhpMyAdmin in Ubuntu 20.04

  1. PhpMyAdmin is a free and open-source web-based PHP application specifically created for administering MySQL/MariaDB database servers through a web browser. It provides an intuitive graphical interface and supports a wide range of common features for database administration tasks. $ sudo apt install phpmyadmin

  2. During the package installation, you will be asked to configure several aspects of the PhpMyAdmin package. First, will be prompted to choose the default web server for running it. Press Esc because NGINX is not on the list provided.

  3. Next, PhpMyAdmin requires a database to work with. In this package configuration prompt, choose Yes to configure a database for PhpMyAdmin with the dbconfig-common package.

  4. In the next prompt, you need to provide a password for PhpMyAdmin to register with the MariaDB database. Enter a secure password and click Enter.

  5. If 403 or 500 error:

sudo rm -rf /usr/share/phpmyadmin/ -R
sudo mkdir /usr/share/phpmyadmin/
cd /usr/share/phpmyadmin/
sudo wget https://files.phpmyadmin.net/phpMyAdmin/5.2.1/phpMyAdmin-5.2.1-all-languages.tar.gz
sudo tar xzf phpMyAdmin-5.2.1-all-languages.tar.gz
sudo mv phpMyAdmin-5.2.1-all-languages/* /usr/share/phpmyadmin

New version could be found here: https://www.phpmyadmin.net/downloads/

Step 6: Configuring NGINX to Serve PhpMyAdmin Site

  1. To enable NGINX to serve the PhpMyAdmin site located at /usr/share/phpmyadmin, create a symlink for this directory under the document root, then set the correct permissions and ownership on the PHPMyAdmin directory as follows.
$ sudo ln -s  /usr/share/phpmyadmin /var/www/html/phpmyadmin
$ sudo chmod 775 -R /usr/share/phpmyadmin/
$ sudo chown root:www-data -R /usr/share/phpmyadmin/
  1. Besides, ensure that the index directive in the default server block configuration (/etc/nginx/sites-available/default) file includes index.php as shown in the following screenshot.

  2. Next, restart the Nginx service once more to apply the above changes. $ sudo systemctl restart nginx

  3. Now access the PhpMyAdmin site from a browser using the following address. http://SERVER_IP/phpmyadmin

At the login page, authenticate with the PHPMyAdmin username and password. Remember the remote root user login is disabled unless you are accessing PHPMyAdmin on the localhost where the MariaDB database is installed, the root access will not work.


Source:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment