Laravel is a free, open-source, and lightweight PHP web framework used for building PHP-based web applications. It is popular due to its elegant syntax, advanced features, and robust toolset. It is based on the Symfony framework and helps developers to simplify web application development. It also provides an Artisan command-line interface to perform operations for your applications. It offers powerful features including, Artisan, MVC Architecture, Object-relational mapping, Template Engine, Unit-Testing, and Database Migration System.

In this post, we will show you how to install Laravel with Nginx on Alma Linux 8.

Prerequisites

  • A server running Alma Linux 8.
  • A valid domain name pointed with your server IP.
  • A root password is configured on your server.

Install LEMP Server

First, you will need to install the Nginx, MariaDB, PHP, and other required PHP extensions on your server. You can install all of them by running the following command:

dnf install nginx mariadb-server php php-fpm php-common php-xml php-mbstring php-json php-zip php-mysqlnd curl unzip -y

After installing all the packages, edit the php-fpm configuration file and configure it to use Nginx:

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

Change the following lines:

listen.owner = nginx
listen.group = nginx

Save and close the file then edit the PHP configuration file and change the default values:

nano /etc/php.ini

Change the following lines:

date.timezone = Asia/Kolkata
cgi.fix_pathinfo=1

Save and close the file then start and enable the Nginx, MariaDB, and PHP-FPM service using the following command:

systemctl start nginx

systemctl start mariadb

systemctl start php-fpm

systemctl enable nginx

systemctl enable mariadb

systemctl enable php-fpm

Once you are finished, you can proceed to the next step.

Install Composer

In this post, we will install Laravel using the Composer. So you will need to install the Composer on your system. You can install it by running the following command:

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

You will get the following output:

All settings correct for using Composer
Downloading...

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

Next, move the Composer binary to the system path and set proper permission with the following command:

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

chmod x /usr/local/bin/composer

Next, verify the Composer version with the following command:

composer --version

You will get the following output:

Composer version 2.2.3 2021-12-31 12:18:53

Install Laravel on Alma Linux 8

Next, change the directory to the Nginx web root directory and install the Laravel using the Composer:

cd /var/www/html/

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

You will get the following output:

Discovered Package: facade/ignition
Discovered Package: fideloper/proxy
Discovered Package: fruitcake/laravel-cors
Discovered Package: laravel/tinker
Discovered Package: nesbot/carbon
Discovered Package: nunomaduro/collision
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.

Next, set proper ownership and permissions 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/

Once you are finished, you can proceed to the next step.

Create an Nginx Virtual Host for Laravel

Next, you will need to create an Nginx configuration file for Laravel. You can create it using the following command:

nano /etc/nginx/conf.d/laravel.conf

Add the following lines:

server {
       listen 80;
       server_name laravel.exampledomain.com;
       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;
        }
}

Save and close the file then verify the Laravel for any configuration error:

nginx -t

You should get the following output:

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

Next, restart the Nginx and PHP-FPM service to apply the changes:

systemctl restart php-fpm

systemctl restart nginx

You can also verify the Nginx status using the following command:

systemctl status nginx

You will get the following output:Advertisement

? nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
  Drop-In: /usr/lib/systemd/system/nginx.service.d
           ??php-fpm.conf
   Active: active (running) since Fri 2022-01-07 08:29:11 UTC; 4s ago
  Process: 8186 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 8184 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 8182 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
 Main PID: 8188 (nginx)
    Tasks: 2 (limit: 11411)
   Memory: 3.7M
   CGroup: /system.slice/nginx.service
           ??8188 nginx: master process /usr/sbin/nginx
           ??8189 nginx: worker process

Jan 07 08:29:11 linux systemd[1]: nginx.service: Succeeded.
Jan 07 08:29:11 linux systemd[1]: Stopped The nginx HTTP and reverse proxy server.
Jan 07 08:29:11 linux systemd[1]: Starting The nginx HTTP and reverse proxy server...
Jan 07 08:29:11 linux nginx[8184]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Jan 07 08:29:11 linux nginx[8184]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Jan 07 08:29:11 linux systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument
Jan 07 08:29:11 linux systemd[1]: Started The nginx HTTP and reverse proxy server.

Configure Firewall For Laravel

Next, you will need to allow ports 80 and 443 through the firewalld firewall. You can allow them with the following command:

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

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

Next, reload the firewalld to apply the changes:

firewall-cmd --reload

Access Laravel Web UI

Now, open your web browser and access the Laravel web UI using the URL http://laravel.exampledomain.com. You should see the Laravel default page on the following screen:

<img alt="Laravel" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/03/echo/p1.png6244365b629c2.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="366" loading="lazy" src="data:image/svg xml,” width=”750″>

Enable SSL on Laravel Website

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 with the following command:

dnf install epel-release -y

dnf install certbot -y

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

certbot --nginx -d laravel.exampledomain.com

You will be asked to provide your valid email and accept the term of service as shown below:

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator nginx, Installer nginx
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): [email protected]

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server at
https://acme-v02.api.letsencrypt.org/directory
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(A)gree/(C)ancel: A

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about our work
encrypting the web, EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for laravel.exampledomain.com
Waiting for verification...
Cleaning up challenges
Deploying Certificate to VirtualHost /etc/nginx/conf.d/laravel.conf

Next, select whether or not to redirect HTTP traffic to HTTPS:

Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2

Type 2 and hit enter to start the process. Once the certificate has been installed, you should see the following output:

Redirecting all traffic on port 80 to ssl in /etc/nginx/conf.d/laravel.conf

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations! You have successfully enabled https://laravel.exampledomain.com

You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=laravel.exampledomain.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/laravel.exampledomain.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/laravel.exampledomain.com/privkey.pem
   Your cert will expire on 2022-04-11. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot again
   with the "certonly" option. To non-interactively renew *all* of
   your certificates, run "certbot renew"
 - Your account credentials have been saved in your Certbot
   configuration directory at /etc/letsencrypt. You should make a
   secure backup of this folder now. This configuration directory will
   also contain certificates and private keys obtained by Certbot so
   making regular backups of this folder is ideal.
 - If you like Certbot, please consider supporting our work by:

   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le

 - We were unable to subscribe you the EFF mailing list because your
   e-mail address appears to be invalid. You can try again later by
   visiting https://act.eff.org.

At this point, your Laravel website is secured with Let’s Encrypt SSL. You can now access it securely using the URL https://laravel.exampledomain.com.Advertisement

Conclusion

Congratulations! you have successfully installed Laravel with Nginx and Let’s Encrypt SSL on Alma Linux 8. you can now start developing PHP-based applications using the Laravel framework. Feel free to ask me if you have any questions.