Advance

How to Installation Laravel PHP with Nginx on CentOS 8

Tutorial installation Laravel PHP framework with Nginx on CentOS 8

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.

Requirements

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.
Recommended Article: Tutorial install and use appimage on ubuntu 20.10

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

First, you need to update system software packages and install LEMP stack, 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

When the installation is finished, start the PHP_PFMNginx, 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.

mysql_secure_installation  
And to secure the server installation, answer the following questions.
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

 

Buy Linux VPS

 

Then, confirm your LEMP stack is running using a browser at your system’s IP address

http://server-IP
Nginx welcome page       

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.

vi /etc/php-fpm.d/www.conf  

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.

vi /etc/php.ini  

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.

 

cgi.fix_pathinfo=1

 

 

fix cgi path info on nginx

 

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

 

security limit extensions on nginx

 

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

install composer on centos 8

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

composer create project on centos 8

In case the process goes well, the application should be installed successfully and a key should be generated as shown in the following screenshot.

composer setting on centos 8

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/

check composer and laravel status on centos 8

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.

vi /etc/nginx/conf.d/mysite.com.conf

By paying attention to the root and fastcgi_pass parameters, copy and paste the following configuration in the file

server { listen 80; server_name mysite.com; root /var/www/html/mysite.com/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; } }

check if the Nginx configuration syntax is correct by running and save the file.

 

nginx -t

 

Nginx Server Block For Laravel

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

Accessing Laravel Website from a Web Browser

It’s time to open a web browser on the local machine and use the following address to navigate.

http://mysite.com

laravel test page after install on centos 8

 

That’s it! you successfully deployed Laravel on CentOS 8. You can now start developing your website or web application using Laravel.

View More Posts
Tom Veitch
Eldernode Writer
We Are Waiting for your valuable comments and you can be sure that it will be answered in the shortest possible time.

Leave a Reply

Your email address will not be published. Required fields are marked *

We are by your side every step of the way

Think about developing your online business; We will protect it compassionately

We are by your side every step of the way

+8595670151

7 days a week, 24 hours a day