We are back with another CentOS tutorials. In this article, we present the tutorial installation Laravel PHP framework with Nginx on CentOS 8. Laravel is an open-source, popular, and modern PHP-based web framework with expressive, elegant, and easy to understand syntax which makes it easy to build large, robust web applications.
Laravel uses the Composer – a PHP package manager for managing dependencies and Artisan – a command-line interface for building and managing web applications.
This simple, fast routing engine, powerful dependency injection container, multiple back-ends for session and cache storage, expressive and intuitive database ORM, robust background job processing, and real-time event broadcasting.
The tutorial may be more useful if you consider :
- PHP >= 7.2.5 with these PHP extensions OpenSSL, PDO, Mbstring, Tokenizer, XML, Ctype and JSON.
- Composer – for installing and manage dependencies.
Tutorial installation Laravel PHP framework with Nginx on CentOS 8
Join us to learn how to install the latest version of the Laravel PHP web framework on CentOS 8 Linux distribution
1- Installing LEMP Stack in CentOS 8
dnf update dnf install nginx php php-fpm php-common php-xml php-mbstring php-json php-zip mariadb-server php-mysqlnd
When the installation is finished, start the PHP_PFM, Nginx, and MariaDB service using the following systemctl commands.
systemctl start php-fpm nginx mariadb systemctl enable php-fpm nginx mariadb systemctl status php-fpm nginx mariadb
By using the security script, you will be able to secure and harden the MariaDB database engine.
Enter current password for root (enter for none): Enter Set root password? [Y/n] y #set new root password Remove anonymous users? [Y/n] y Disallow root login remotely? [Y/n] y Remove test database and access to it? [Y/n] y Reload privilege tables now? [Y/n] y
In case your service is running the firewalls, open the HTTP and HTTPS service in the firewall to enable client requests to the Nginx webserver
firewall-cmd --zone=public --permanent --add-service=http firewall-cmd --zone=public --permanent --add-service=https firewall-cmd --reload
Then, confirm your LEMP stack is running using a browser at your system’s IP address
2- Configuring and Securing PHP-FPM and Nginx
Also, PHP-FPM can listen on a Unix socket or TCP socket to process requests from the Nginx web server. And this is defined by the listen parameter in the /etc/php-fpm.d/www.conf configuration file.
You need to set the correct ownership and permissions on it as shown in the screenshot if you are using a Unix socket. Therefore, uncomment the following parameters and set their values to the user and group to match the user and group Nginx is running as.
listen.owner = nginx listen.group = nginx listen.mode = 066
Also, you need to set the system-wide time zone in the /etc/php.ini configuration file.
To continue, Look for the line “;date.timezone” and uncomment it, then set its value as shown in the screenshot.
date.timezone = Africa/Kampala
If you want to get rid of Nginx passing requests from malicious users who use other extensions to execute PHP code to PHP-FPM, uncomment the following parameter and set its value to 0.
You can uncomment the following parameter in the /etc/php-fpm.d/www.conf file. Read the comment for more explanation.
security.limit_extensions = .php .php3 .php4 .php5 .php7
3- Installing Composer and Laravel PHP Framework
Use the following commands to install the Composer package. The first command downloads the installer, then runs it using PHP.
curl -sS https://getcomposer.org/installer | php mv composer.phar /usr/local/bin/composer chmod +x /usr/local/bin/composer
As the Composer is installed, you can use it to install Laravel files and dependencies as follows.
cd /var/www/html/ composer create-project --prefer-dist laravel/laravel mysite.com
In case the process goes well, the application should be installed successfully and a key should be generated as shown in the following screenshot.
Now the .env environment file was created and the required application was also generated. during the installation. And you should not create them manually like before. To confirm this, run a long listing of the laravel root directory using ls command.
ls -la mysite.com/
Then, start to configure the correct ownership and permissions on the storage and the bootstrap/cache directories to be writable by the Nginx web server.
chown -R :nginx /var/www/html/mysite.com/storage/ chown -R :nginx /var/www/html/mysite.com/bootstrap/cache/ chmod -R 0777 /var/www/html/mysite.com/storage/ chmod -R 0775 /var/www/html/mysite.com/bootstrap/cache/
You need to update the security context of the storage and bootstrap/cache directories if SELinux enabled on your server.
semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/html/mysite.com/storage(/.*)?' semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/html/mysite.com/bootstrap/cache(/.*)?' restorecon -Rv '/var/www/html/mysite.com'
4- Configure Nginx Server Block For Laravel
To let the Nginx start serving your website or application, create a server block for it in a .conf file under /etc/nginx/conf.d/ directory.
By paying attention to the root and fastcgi_pass parameters, copy and paste the following configuration in the file
check if the Nginx configuration syntax is correct by running and save the file.
Then restart the PHP-FPM and Nginx services for the recent changes to take effect.
systemctl restart php-fpm systemctl restart Nginx
5- Accessing Laravel Website from a Web Browser
You need to use the /etc/hosts file on your local machine to create local DNS to access the Laravel website at mysite.com, which is not a fully qualified domain name (FQDN) and it is not registered.
To add the server IP address and domain in the required file, run the following command.
ip add #get remote server IP echo "10.42.0.21 mysite.com" | sudo tee -a /etc/hosts
It’s time to open a web browser on the local machine and use the following address to navigate.
That’s it! you successfully deployed Laravel on CentOS 8. You can now start developing your website or web application using Laravel.
Dear user, we wish this tutorial would be helpful for you, to ask any question or review the conversation of our users about this article, please visit Ask page. Also to improve your knowledge, there are so many useful tutorials ready for Eldernode training.