Laravel is an open-source, well-known, and modern PHP-based web framework with expressive, elegant, and easy to understand syntax which makes it easy to build large, robust web applications.
Its key features include a simple, fast routing engine, powerful dependency injection container, multiple back-ends for session and cache storage, expressive and intuitive database ORM (Object-relational Mapping), robust background job processing, and real-time event broadcasting.
Also, it uses tools such as Composer – a PHP package manager for managing dependencies and Artisan – a command-line interface for building and managing web applications.
You will learn how to install the latest version of the Laravel PHP web framework on CentOS 8 Linux distribution.
Install LEMP Stack
1. To start off, update system software packages and install LEMP stack (Linux, Nginx, MariaDB / MySQL, and PHP) using the following dnf commands.
# dnf update # dnf install nginx php php-fpm php-common php-xml php-mbstring php-json php-zip mariadb-server php-mysqlnd
2. When the LEMP installation is complete, you need to start the PHP-PFM, Nginx and MariaDB services using the following systemctl commands.
# systemctl start php-fpm nginx mariadb # systemctl enable php-fpm nginx mariadb # systemctl status php-fpm nginx mariadb
3. You need to secure and harden the MariaDB database engine using the security script as shown.
Answer the following questions to secure the server installation.
Enter current password for root (enter for none): press Enter Set root password? [Y/n]
y#set new root password Remove anonymous users? [Y/n] press y Disallow root login remotely? [Y/n]
yRemove test database and access to it? [Y/n] press y Reload privilege tables now? [Y/n]
4. If you have the firewalld service running, you need to open the HTTP and HTTPS service in the firewall to enable client requests to the Nginx web server.
# firewall-cmd --zone=public --permanent --add-service=http # firewall-cmd --zone=public --permanent --add-service=https # firewall-cmd --reload
5. Finally, you can confirm that your LEMP stack is running using a browser at your system’s IP address.
Configuring and Securing PHP-FPM and Nginx
1. To process requests from the Nginx web server, PHP-FPM can listen on a Unix socket or TCP socket and this is defined by the listen parameter in the /etc/php-fpm.d/www.conf configuration file.
# vi /etc/php-fpm.d/www.conf
By default, it is configured to listen on a Unix socket as shown in the following screenshot. The value here will be specified in the Nginx server block file later on.
2. If using a Unix socket, you should also set the correct ownership and permissions on it as shown in the screenshot. 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
3. Next, also set the system-wide time zone in the /etc/php.ini configuration file.
# vim /etc/php.ini
Look for the line “date.timezone” and uncomment it, then set its value as shown in the screenshot (use values that apply to your region/continent and country).
date.timezone = Africa/Cairo
4. To mitigate the risk of Nginx passing requests from malicious users who use other extensions to execute PHP code to PHP-FPM, uncomment the following parameter and set it’s value to 0 .
5. In relation to the previous point, also 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
Install Composer and Laravel PHP Framework
1. Next, install the Composer package by running the following commands. The first command downloads the installer, then runs it using PHP.
[unixcop@unixcop ~]$ curl -sS https://getcomposer.org/installer | php All settings correct for using Composer Downloading… Composer (version 2.1.5) successfully installed to: /home/unixcop/composer.phar Use it: php composer.phar [unixcop@unixcop ~]$
Then run this commands :
mv composer.phar /usr/local/bin/composer chmod +x /usr/local/bin/composer
2. Now that Composer is installed, use it to install Laravel files and dependencies as follows. Replace newunixcop.com with the name of the directory where the Laravel files will be stored, the absolute path (or root path in Nginx configuration file) will be /var/www/html/newunixcop.com.
# cd /var/www/html/ # composer create-project --prefer-dist laravel/laravel newunixcop.com
3. During the installation process, the .env environment file was created and the required application was also generated, so you don’t need to create them manually like before. To confirm this, run a long listing of the laravel root directory using ls command.
# ls -la newunixcop.com/
4. You need to configure the correct ownership and permissions on the storage and the bootstrap/cache directories to be writable by the Nginx web server.
[root@unixcop html]# chown -R :nginx /var/www/html/newunixcop.com/storage/ [root@unixcop html]# chown -R :nginx /var/www/html/newunixcop.com/bootstrap/cache/ [root@unixcop html]# chown -R :nginx /var/www/html/newunixcop.com/storage/ [root@unixcop html]# chmod -R 0775 /var/www/html/newunixcop.com/bootstrap/cache/
5. If SELinux enabled on your server, you should also update the security context of the storage and bootstrap/cache directories.
run this command :
[root@unixcop html]# restorecon -Rv '/var/www/html/newunixcop.com' Relabeled /var/www/html/newunixcop.com/storage/logs from unconfined_u:object_r:httpd_sys_content_t:s0 to unconfined_u:object_r:httpd_log_t:s0 Relabeled /var/www/html/newunixcop.com/storage/logs/.gitignore from unconfined_u:object_r:httpd_sys_content_t:s0 to unconfined_u:object_r:httpd_log_t:s0 [root@unixcop html]#
Configure Nginx Server Block For Laravel
1. For Nginx to start serving your website or application, you need to create a server block for it in a .conf file under /etc/nginx/conf.d/ directory as shown.
# vim /etc/nginx/conf.d/newunixcop.com.conf
Copy and paste the following configuration in the file. Take note of the root and fastcgi_pass parameters.
2. Save the file and check if the Nginx configuration syntax is correct by running.
[root@unixcop ~]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [root@unixcop ~]#
3. Then restart the PHP-FPM and Nginx services for the recent changes to take effect.
# systemctl restart php-fpm # systemctl restart Nginx
Accessing Laravel Website from a Web Browser
4. To access the Laravel website at newunixcop.com, which is not a fully qualified domain name (FQDN) and it is not registered (it’s only used for testing purposes), we will use the /etc/hosts file on your local machine to create local DNS.
Run the following command to add the server IP address and domain in the required file (replace the value according to your settings).
[root@unixcop ~]# echo "192.168.122.60 newunixcop.com" | sudo tee -a /etc/hosts 192.168.122.60 newunixcop.com [root@unixcop ~]#
5. Open a web browser on the local machine and use the following address to navigate.
You have successfully deployed Laravel on CentOS 8. You can now start developing your website or web application using Laravel. For more information, see Laravel Getting Started guide.