Laravel is an open-source PHP framework designed for web application development. It is built on various components of the Symfony framework and makes it easier to program PHP web applications. It provides a meaningful and creative syntax for simplifying common tasks such as authentication, routing, sessions, working with databases, and more. It makes development process simple for developers without compromising program capabilities.

In this post, we will show you how to install Laravel PHP Framework on Rocky Linux 8.

Step 1 – Install Nginx, MariaDB and PHP

First, you will need to install the Nginx web server, MariaDB database server, PHP, and other PHP extensions to your server. You can install all of them using 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

Once all the packages are installed, start and enable the Nginx, MariaDB and PHP-FPM services using the following command:

systemctl start php-fpm nginx mariadb
systemctl enable php-fpm nginx mariadb

Next, you will need to edit the PHP-FPM configuration file and change the user and group from apache to 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.ini file:

nano /etc/php.ini

Change the following lines:

date.timezone = UTC
cgi.fix_pathinfo=1

Save and close the file, then restart the PHP-FPM service to apply the changes.

systemctl restart php-fpm

Step 2 – Install Composer

Composer is a dependency manager for PHP. It is used to install various PHP dependencies required for your project. You can install it using the following command:

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

You should get the following output:

All settings correct for using Composer
Downloading...

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

Next, move the downloaded binary to the system path and set proper permission:

mv composer.phar /usr/local/bin/composer
chmod +x /usr/local/bin/composer

Now, verify the Composer version with the following command:

composer --version

You should get the following output:

Composer version 2.1.6 2021-08-19 17:11:08

Step 3 – Install Laravel

First, navigate to Nginx web root directory and download the latest version of Laravel using the following command:

cd /var/www/html/
composer create-project --prefer-dist laravel/laravel laravelsite

You should get the following output:

Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover --ansi
Discovered Package: facade/ignition
Discovered Package: fruitcake/laravel-cors
Discovered Package: laravel/sail
Discovered Package: laravel/sanctum
Discovered Package: laravel/tinker
Discovered Package: nesbot/carbon
Discovered Package: nunomaduro/collision
Package manifest generated successfully.
76 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
> @php artisan vendor:publish --tag=laravel-assets --ansi
No publishable resources for tag [laravel-assets].
Publishing complete.
> @php artisan key:generate --ansi
Application key set successfully.

Next, set proper permissions and ownership to laravel directories:

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

Step 4 – Configure Nginx for Laravel

Now, you will need to create an Nginx virtual host configuration file to host Laravel on the Internet.

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.yourdomain.com;
       root        /var/www/html/laravelsite/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 Nginx for any syntax error susing the following command:

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

Finally, restart both the Nginx and PHP-FPM services 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 should see the following output:

● 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 Tue 2021-09-14 09:36:05 UTC; 4s ago
  Process: 17025 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 17023 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 17020 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
 Main PID: 17026 (nginx)
    Tasks: 3 (limit: 23695)
   Memory: 5.0M
   CGroup: /system.slice/nginx.service
           ├─17026 nginx: master process /usr/sbin/nginx
           ├─17027 nginx: worker process
           └─17028 nginx: worker process

Step 5 – Configure Firewall

If firewalld is installed and running in your server then you will need to allow HTTP and HTTPS service through the firewall. You can allow both services using 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

Step 6 – Access Laravel Dashboard

You can now access the Laravel dashboard using the URL http://laravel.yourdomain.com. You will be redirected to the Laravel default screen:
Laravel dashboard page

Conclusion

Congratulations! You have successfully installed Laravel with Nginx on Rocky Linux 8. You can now start working smoothly to create PHP web applications on this framework. Get started on dedicated hosting from Atlantic.Net.