In this guide, we intend to teach you How To Install and Configure Laravel on Rocky Linux 8. Also, you will learn to Secure Laravel on Rocky Linux 8.
Laravel is an open-source PHP framework, which is robust and easy to understand. It follows a model-view-controller design pattern. Laravel reuses the existing components of different frameworks which helps in creating a web application. The web application thus designed is more structured and pragmatic.
Steps To Install and Configure Laravel on Rocky Linux 8
To complete this guide, you need some requirements.
Requirements
First, you must log in to your server as a non-root user with sudo privileges and set up a basic firewall. To do this, you can follow our guide on Initial Server Setup with Rocky Linux 8.
Then, you should have a LEMP stack installed on your server. To do this, you can follow this guide on How To Install LEMP Stack on Rocky Linux 8.
Also, you need a domain name that is pointed to your server’s IP address.
Now follow the steps below to complete this guide.
Install PHP Extensions on Rocky Linux 8
First, you need to install some PHP extensions and required packages on your server with the command below:
sudo dnf install php-common php-xml php-mbstring php-json php-zip curl unzip -y
Edit php-fpm Configuration File
At this point, you need to make some configuration changes to the php-fpm file on Rocky Linux 8. To do this, open the file with your favorite text editor, here we use vi:
sudo 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.
Edit php.ini Configuration File
At this point, you need to edit the PHP configuration file:
sudo vi /etc/php.ini
And change the following lines. Set your own time zone, and 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.
Install Composer on Rocky Linux 8
In this guide, you will install Laravel by using the composer. So you need to install Composer on Rocky Linux 8 with the command below:
sudo 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.4.4) successfully installed to: /root/composer.phar
Use it: php composer.phar
Now you need to move the Composer binary to the system path:
sudo mv composer.phar /usr/local/bin/composer
Then, set the correct permissions for it:
sudo chmod +x /usr/local/bin/composer
Verify your Composer installation on Rocky Linux 8 by checking its version:
composer --version
Output
Composer version 2.4.4 2022-10-27 14:39:29
Install Laravel on Rocky Linux 8
At this point, you can start to install Laravel on your server.
First, switch to the Nginx web root directory:
sudo cd /var/www/html/
Then, install Laravel by using the Composer on Rocky Linux 8:
sudo composer create-project --prefer-dist laravel/laravel laravel
When your installation is completed, you will get the following output:
Output
INFO Application key set successfully.
Then, use the following commands to set the correct permissions and ownership to Laravel on Rocky Linux 8:
# sudo chown -R nginx:nginx /var/www/html/laravel/ # sudo chown -R nginx:nginx /var/www/html/laravel/storage/ # sudo chown -R nginx:nginx /var/www/html/laravel/bootstrap/cache/ # sudo chmod -R 0777 /var/www/html/laravel/storage/ # sudo chmod -R 0775 /var/www/html/laravel/bootstrap/cache/
Create an Nginx Configuration file for Laravel on Rocky Linux 8
At this point, you need to create an Nginx configuration file for Laravel, here we use vi:
sudo 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 Rocky Linux 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:
# sudo systemctl restart php-fpm # sudo systemctl restart nginx
Configure Firewall
Next, you need to allow ports 80 and 443 through the firewall. To do this, run the commands below:
# sudo firewall-cmd --zone=public --permanent --add-service=http # sudo firewall-cmd --zone=public --permanent --add-service=https
Reload the firewall, to apply the new rules:
sudo 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
Secure Laravel with Let’s Encrypt on Rocky Linux 8
It is recommended to enable 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 Rocky Linux 8 with the following commands:
sudo dnf install epel-release -y sudo dnf install certbot -y
Then, run the following command to download Let’s Encrypt SSL for your Laravel domain:
sudo 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 Rocky Linux 8.
You can now access it securely using the URL below:
https://domain-name
Conclusion
At this point, you learn to Install, Configure, and Secure Laravel on Rocky Linux 8.
Hope you enjoy it.
You may be like these articles: