status_mariadb

(LEMP) Installing Laravel, PHP 7.4, Mariadb 10.5 and NGINX on Ubuntu 20.04 Desktop

In this blog, we’re not just going to give details about Laravel but provide information as well on how to install LEMP. I know frustrations one sometimes have when using Nginx, PHP, and MySQL/MariaDB like “wt* is wrong with permissions?! I’ve already added it to groups in www-data, I’ve already set my folder to 777, etc” well, I’m with you on that, that is why this tutorial will help you cover everything and starts working on LEMP environment.

Step 1: Install NGINX 

sudo apt update && sudo apt install nginx 

sudo apt install vim-nox

Configure Nginx 

Next, open the Nginx configuration file, which can be found at /etc/nginx/nginx.conf 

sudo vi /etc/nginx/nginx.conf 

Start by setting the user to the username that you’re currently logged in with. This will make managing file permissions much easier in the future. 

The worker_processes directive is the amount of CPU cores your instance. In my case, this is 1. 

Uncomment the multi_accept directive and set it to on. 

Lower the keepalive_timeout directive to 15. 

For security reasons, you should uncomment the server_tokens directive and ensure it is set to off. 

Add the new client_max_body_size directive below the server_tokens and set the value to 64m. 

Uncomment the gzip_proxied directive and set it to any, uncomment the gzip_comp_level directive and set it to the value of 2 and finally uncomment the gzip_types directive. 

In order for Nginx to correctly serve PHP you also need to ensure the fastcgi_param SCRIPT_FILENAME directive is set, otherwise, you will receive a blank white screen when accessing any PHP scripts. So open fastcgi_params file by issuing 

sudo vi /etc/nginx/fastcgi_params 

Add the following at the end of the file 

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 

That’s all, this is the basic Nginx configuration, hit CTRL+X followed by Y to save the changes. Ensure that the configuration file contains no errors and restart Nginx for the changes to take effect by issuing the following command 

sudo nginx -t 

If you get a successful message, then proceed with the following command 

sudo service nginx restart 

Step 2: Install PHP 7.4 FPM and Related Modules 

sudo apt-get install php7.4-fpm php7.4-cli php7.4-mysql php7.4-gd php7.4-imagick php7.4-tidy php7.4-xmlrpc php7.4-common php7.4-curl php7.4-mbstring php7.4-xml php7.4-bcmath php7.4-bz2 php7.4-intl php7.4-json php7.4-readline php7.4-zip curl 

To check if PHP 7.4 was installed correctly 

php –v 

Now that PHP 7.4.* has installed and you need to configure the user and group that the service will run under. 

sudo vi /etc/php/7.4/fpm/pool.d/www.conf 

Change the following lines by replacing the www-data with your username. 

user = username 
group = username 
listen.owner = username 
listen.group = username 

Now we configure PHP for laravel by changing some values in php.ini. 

sudo vi /etc/php/7.4/fpm/php.ini 

upload_max_filesize = 32M 
post_max_size = 48M 
memory_limit = 256M 
max_execution_time = 600 
max_input_vars = 3000 
max_input_time = 1000 

Hit CTRL+X and Y to save the configuration and check if the configuration is correct and restart PHP 

sudo php-fpm7.4 -t 
sudo service php7.4-fpm restart 

Step 3: Install Maridb 10.5 

sudo apt -y install software-properties-common
sudo apt-key adv --fetch-keys 'https://mariadb.org/mariadb_release_signing_key.asc'
sudo add-apt-repository 'deb [arch=amd64] http://mariadb.mirror.globo.tech/repo/10.5/ubuntu focal main'
sudo apt update
sudo apt install mariadb-server mariadb-client

sudo systemctl status mariadb 

sudo mysql_secure_installation

The script will prompt you to set up the root user password, remove the anonymous user, restrict root user access to the local machine and remove the test database. At the end the script will reload the privilege tables ensuring that all changes take effect immediately.

All steps are explained in detail and it is recommended to answer “Y” (yes) to all questions.

Step 5: Install phpMyadmin

sudo apt install phpmyadmin

During the installation, it will prompt you to select a web server to configure. Nginx isn’t in the list, so press the Tab key and hit OK to skip this step.

Next, select Yes to create a new database and let dbconfig-common to configure it.

This will also create a new database user named phpmyadmin. Give this user a password.

Step 6: Create Nginx Server Block for phpMyadmin

sudo vi /etc/nginx/conf.d/phpmyadmin.conf

 

server {
  listen 80;
  listen [::]:80;
  server_name phpmyadmin.test;
  root /usr/share/phpmyadmin/;
  index index.php index.html index.htm index.nginx-debian.html;
  access_log /var/log/nginx/phpmyadmin_access.log;
  error_log /var/log/nginx/phpmyadmin_error.log;
  location / {
    try_files $uri $uri/ /index.php;
  }
  location ~ ^/(doc|sql|setup)/ {
    deny all;
  }
  location ~ .php$ {
    fastcgi_pass unix:/run/php/php7.4-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
    include snippets/fastcgi-php.conf;
  }
  location ~ /.ht {
    deny all;
  }
}

sudo nano /etc/hosts

Add phpmyadmin.test on your host file

sudo nginx -t

sudo nginx -s reload

Open http://phpmyadmin.test/

Step 7: Create Directory and Grant Permission

sudo chown -R $(whoami):$(whoami) /var/www/html/

sudo chmod -R 755 /var/www

Step 8: Install Composer

curl -sS https://getcomposer.org/installer | php

sudo mv composer.phar /usr/local/bin/composer
sudo chmod +x /usr/local/bin/composer
source ~/.bashrc

Step 9: Install Laravel

cd /var/www/html

sudo composer clearcache

composer create-project –prefer-dist laravel/laravel inventory

After running the commands above, a new project directory will be created… Run the commands below to set the correct permissions for that directory

sudo chmod -R 755 /var/www/html/inventory/

Step 10: Server Configuration

sudo nano /etc/nginx/sites-available/inventory.test

server {
    listen   80;
    listen   [::]:80;
    server_name inventory.test;
    root   /var/www/html/inventory/public;
    index  index.php;    location / {	try_files $uri $uri/ /index.php?$query_string;
    }
error_page 404 /index.php; location ~ .php$ { fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name; include fastcgi_params; } location ~ /.(?!well-known).* { deny all; }}

sudo ln -s /etc/nginx/sites-available/inventory.test /etc/nginx/sites-enabled/

sudo vi /etc/hosts

Test configuration if its all working.

sudo nginx –t

sudo systemctl restart nginx

Step 11: Testing laravel on the Browser

You can now view this page in your web browser by visiting your server’s domain name or public IP address http://inventory.test/