Skip to content

Instantly share code, notes, and snippets.

@reanim8ed
Created July 20, 2023 16:18
Show Gist options
  • Save reanim8ed/b9c7fb9d8d6ead8f749f0290bd20be69 to your computer and use it in GitHub Desktop.
Save reanim8ed/b9c7fb9d8d6ead8f749f0290bd20be69 to your computer and use it in GitHub Desktop.
[LEMP on Ubuntu 22.04] #ubuntu

LEMP - Nginx Web Server, MySQL / MariaDB Database, and PHP

Step 1: Update Software Repositories

sudo apt update
sudo apt upgrade

Step 2: Install Nginx Web Server on Ubuntu 22.04 LTS

sudo apt install nginx

Step 3: Install MariaDB Server on Ubuntu 22.04 LTS

sudo apt install mariadb-server

Step 3.1: Securing the MariaDB Server

Next, we’ll use a script (mysql_secure_installation) provided by the mariadb-server package to restrict access to the server and remove unused accounts because the default setup makes your MariaDB installation unsafe.

sudo mysql_secure_installation After running the above command, you will be prompted to enter the MariaDB root password. Just leave the root password empty, and press the Enter key. For the rest, type Y and hit Enter. We clarify that the password specified above for the MariaDB root accounts is only for remote users. To log in from the host we installed it on, you do not need to enter a password and will not be asked for one.

Step 3.2: Testing MariaDB Installation

To check if the database server is functioning normally: sudo mysql

The server console should come up. Then, run a simple query: select version(); In response to your query, the MariaDB server should return its version. Finally, to exit the MariaDB shell and return to the system terminal, use the quit command.

Step 4: Install PHP on Ubuntu 22.04 LTS

sudo apt install php-fpm php-mysql php-gd php-cli php-curl php-mbstring php-zip php-opcache

Step 5: Configure Nginx to Execute PHP Files

Now that we’ve installed all of the LEMP components on our Ubuntu 22.04 system, we need to edit the default Nginx virtual host configuration file.

sudo vim /etc/nginx/sites-enabled/default Add the following lines to the default server block to allow Nginx to process PHP files:

location ~ \.php$ {
     include snippets/fastcgi-php.conf;
     fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
}

Test the modified Nginx configuration file for syntax errors by entering the following command: sudo nginx -t

When you are ready, restart Nginx to make the changes take effect: sudo systemctl restart nginx

Step 6: Test your Ubuntu 22.04 LEMP Installation

Finally, let’s create a test PHP file to verify that PHP-FPM works and is integrated with Nginx. In the default server block above, our site is being served from /var/www/html, so we’ll create a test file there:

echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/test.php Now, you can access “test.php” from a web browser, using your site’s domain or server’s IP address followed by “/test.php.” A web page with complete information about your PHP installation appear.

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