Sunday, September 25, 2022

How To Install and Secure Laravel on AlmaLinux 8

In this article, we want to teach you How To Install and Secure Laravel on AlmaLinux 8.

Laravel is an open-source PHP framework designed to make developing web apps easier and faster through built-in features.

It is a server-side PHP framework; with it you can build full-stack apps, meaning apps with features typically requiring a backend, such as user accounts, exports, order management, etc.

How To Install and Secure Laravel on AlmaLinux 8

Before you start to install Laravel on AlmaLinux 8, you need some requirements first.

Requirements

First, you need to log in to your server as a root or non-root user with sudo privileges and set up a basic firewall. To do this, you can follow our article the Initial Server Setup with AlmaLinux 8.

Then, you need to have the LEMP installed on your server. For this, you can follow the article How To Install LEMP Stack on AlmaLinux 8.

Also, you need a domain name that pointed to your server’s IP address.

When you are done with these requirements, you can follow the steps below to complete this guide.

Install Additional PHP extensions on AlmaLinux 8

First, you need to install some PHP extensions and required packages on your server with the command below:

dnf install php-common php-xml php-mbstring php-json php-zip curl unzip -y

Then, you need to edit the PHP-FPM configuration file:

vi /etc/php-fpm.d/www.conf

Find the lines below and uncomment them by removing the “;” from the beginning of the line and changing them to the Nginx:

listen.owner = nginx
listen.group = nginx

When you are done, save and close the file.

Next, you need to edit the PHP configuration file:

vi /etc/php.ini

And change the following lines. Set your own time zone, uncomment them by removing the “;” from the beginning of the line:

date.timezone = America/New_York
cgi.fix_pathinfo=1

When you are done, save and close the file.

Installing Composer

In this guide, you will install Laravel by using the composer. So you need to install Composer on AlmaLinux 8 with the command below:

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

When your installation is completed, you will get the following output:

Output
All settings correct for using Composer
Downloading...

Composer (version 2.3.5) successfully installed to: /root/composer.phar
Use it: php composer.phar

Now you need to move the Composer binary to the system path:

mv composer.phar /usr/local/bin/composer

Then, set the correct permissions for it:

chmod +x /usr/local/bin/composer

Verify your “Composer installation by checking its version:

composer --version
Output
Composer version 2.3.5 2022-04-13 16:43:00

Install Laravel on AlmaLinux 8

At this point, you can start to install Laravel on your server.

First, switch to the Nginx web root directory:

cd /var/www/html/

Then, install Laravel by using the Composer:

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

When your installation is completed, you will get the following output:

Output
Package manifest generated successfully.
69 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
> @php artisan key:generate --ansi
Application key set successfully.

Then, use the following commands to set the correct permissions and ownership to Laravel:

# chown -R nginx:nginx /var/www/html/laravel/
# chown -R nginx:nginx /var/www/html/laravel/storage/
# chown -R nginx:nginx /var/www/html/laravel/bootstrap/cache/
# chmod -R 0777 /var/www/html/laravel/storage/
# chmod -R 0775 /var/www/html/laravel/bootstrap/cache/

Create an Nginx VirtualHost for Laravel

At this point, you need to create an Nginx configuration file for Laravel, here we use vi:

vi /etc/nginx/conf.d/laravel.conf

Add the following content to the file, remember to replace the domain name with your own:

server {
       listen 80;
       server_name domain-name;
       root        /var/www/html/laravel/public;
       index       index.php;
       charset utf-8;
       gzip on;
	gzip_types text/css application/javascript text/javascript application/x-javascript  image/svg+xml text/plain text/xsd text/xsl text/xml image/x-icon;
        location / {
        	try_files $uri $uri/ /index.php?$query_string;
        }

        location ~ \.php {
                include fastcgi.conf;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/run/php-fpm/www.sock;
        }
        location ~ /\.ht {
                deny all;
        }
}

When you are done, save and close the file.

Then, verify Laravel for any configuration error on AlmaLinux 8:

nginx -t
Output
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

To apply the changes, restart Nginx and PHP-FPM:

# systemctl restart php-fpm
# systemctl restart nginx

Next, you need to allow ports 80 and 443 through the firewall. To do this, run the commands below:

# firewall-cmd --zone=public --permanent --add-service=http
# firewall-cmd --zone=public --permanent --add-service=https

Reload the firewall, to apply the new rules:

firewall-cmd --reload

Access Laravel Web Interface

At this point, you can access your Laravel through the web interface by typing your domain name or server’s IP address:

http://domain-name

You will see the following screen:

Laravel screen

Secure Laravel with Let’s Encrypt on AlmaLinux 8

It is recommended to enable the SSL on the Laravel website to secure the connection. Let’s Encrypt provides a free SSL to obtain, renew, and manage SSL/TLS certificates for your domain.

First, install the Certbot client on AlmaLinux 8 with the following commands:

dnf install epel-release -y
dnf install certbot -y

Then, run the following command to download Let’s Encrypt SSL for your Laravel domain:

certbot --nginx -d domain-name

You will be asked to provide your valid email and accept the term of service.

Next, select whether or not to redirect HTTP traffic to HTTPS. Type 2 and hit enter to start the process.

When the certificate has been installed, you should see the following output:

At this point, your Laravel website is secured with Let’s Encrypt SSL on AlmaLinux 8.

You can now access it securely using the URL below:

https://domain-name

Conclusion

At this point, you learn to Install and Secure Laravel on AlmaLinux 8.

Hope you enjoy it.

May this article about How To Set up Laravel on Ubuntu 20.04 be useful for you.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles

POPULAR TAGS

Most Popular