Laravel is a powerful and popular PHP web framework that simplifies web application development. With its elegant syntax, advanced features, and robust toolset, Laravel has gained a strong following among developers. In this article, we will guide you through the process of installing and securing Laravel with Nginx on AlmaLinux 8. By following these steps, you will have a secure and optimized environment to build your PHP-based web applications.
Prerequisites
Before we begin, make sure you have the following prerequisites in place:
- A server running AlmaLinux 8
- A valid domain name pointed to your server’s IP address
- Root access to your server
Step 1: Install LEMP Server
To install Laravel, we first need to set up the LEMP (Linux, Nginx, MariaDB, PHP) stack on our server. The LEMP stack provides the necessary components to run a PHP web application. To install the LEMP stack, open a terminal and run 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
This command will install Nginx, MariaDB, PHP, and other required PHP extensions on your server.
Once the installation is complete, we need to configure PHP-FPM to work with Nginx. Open the PHP-FPM configuration file with a text editor:
nano /etc/php-fpm.d/www.conf
In this file, locate the following lines:
listen.owner = nginx listen.group = nginx
Change the owner and group to “nginx”. Save and close the file.
Next, edit the main PHP configuration file:
nano /etc/php.ini
Find the following lines:
date.timezone = Asia/Kolkata cgi.fix_pathinfo=1
Set your desired timezone and change the value of cgi.fix_pathinfo
to “1”. Save and close the file.
Start and enable the Nginx, MariaDB, and PHP-FPM services:
systemctl start nginx systemctl start mariadb systemctl start php-fpm systemctl enable nginx systemctl enable mariadb systemctl enable php-fpm
Now that the LEMP stack is installed and configured, we can proceed to the next step.
Step 2: Install Composer
Composer is a dependency management tool widely used in the PHP community. We will use Composer to install Laravel. To install Composer, run the following command:
curl -sS https://getcomposer.org/installer | php
This command downloads the Composer installer and installs it on your system. Next, move the Composer binary to the system path and make it executable:
mv composer.phar /usr/local/bin/composer chmod +x /usr/local/bin/composer
Verify the Composer installation by checking its version:
composer --version
If you see the Composer version displayed, you have successfully installed Composer.
Step 3: Install and Configure Laravel
Now that we have Composer installed, we can use it to install Laravel. Change to the Nginx web root directory:
cd /var/www/html
Use Composer to create a new Laravel project named “laravel”:
composer create-project--prefer-dist laravel/laravel laravel
This command downloads and installs the latest version of Laravel in a directory named “laravel”.
Next, we need to set the correct ownership and permissions for the Laravel files. Run the following commands:
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
These commands ensure that the Nginx user has the necessary permissions to read and write files in the Laravel directories.
Step 4: Configure Nginx for Laravel
To serve Laravel with Nginx, we need to create an Nginx configuration file specifically for Laravel. Open a new configuration file using a text editor:
nano /etc/nginx/conf.d/laravel.conf
Add the following configuration to the file:
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; } }
In this configuration, replace “laravel.exampledomain.com” with your actual domain name. Save and close the file.
To verify the Nginx configuration, run the following command:
nginx -t
If there are no syntax errors in your configuration, you should see a message indicating that the configuration file test is successful.
Restart Nginx and PHP-FPM to apply the changes:
systemctl restart php-fpm
systemctl restart nginx
Verify the status of Nginx to ensure it is running:
systemctl status nginx
If Nginx is running, you should see a status message indicating that the service is active.
Step 5: Configure Firewall for Laravel
To allow incoming HTTP and HTTPS traffic to your Laravel website, you need to configure the firewall. Run the following commands to open the necessary ports:
firewall-cmd --zone=public --permanent --add-service=http firewall-cmd --zone=public --permanent --add-service=https
Reload the firewall to apply the changes:
firewall-cmd --reload
Your firewall is now configured to allow HTTP and HTTPS traffic to reach your Laravel website.
Step 6: Access Laravel Web UI
Now that you have installed and configured Laravel with Nginx, you can access the Laravel web interface. Open your web browser and enter the URL http://laravel.exampledomain.com
. You should see the Laravel default page, indicating that your installation was successful.
Step 7: Enable SSL with Let’s Encrypt
To secure your Laravel website with SSL, we will use Let’s Encrypt, a free and widely trusted certificate authority. Let’s Encrypt provides automated tools to obtain and manage SSL/TLS certificates. To install the Certbot client, run the following commands:
dnf install epel-release -y dnf install certbot -y
Once Certbot is installed, obtain a Let’s Encrypt SSL certificate for your Laravel domain by running the following command:
certbot --nginx -d laravel.exampledomain.com
Follow the prompts to provide your email address and agree to the terms of service. Certbot will automatically configure Nginx to use the SSL certificate for your Laravel website.
After the installation is complete, your Laravel website will be accessible over HTTPS. You can test the SSL installation by visiting https://laravel.exampledomain.com
in your web browser.
Conclusion
Congratulations! You have successfully installed and secured Laravel with Nginx on AlmaLinux 8. You now have a robust and optimized environment to develop your PHP-based web applications. By following the steps outlined in this article, you have created a secure and scalable foundation for your Laravel projects. If you have any questions or need further assistance, feel free to reach out to the Shape.host support team. Shape.host provides reliable and efficient Linux SSD VPS hosting services, empowering businesses with secure and high-performance cloud hosting solutions.