Running Laravel with Nginx on Fedora Server

Hello guys, this time I will give tutorial about running Laravel App using Nginx on Fedora Server. For this tutorial I use Laravel 8, Nginx 1.20.1, Fedora 33 (Server Edition). I will breakdown this tutorial into several steps. Before we start to deploy, you should install Putty first on your local computer and make sure you have Git installed on your server. Ok, let’s start :

STEP 1 (Install PHP and Composer)

To run Laravel App on the server, you need to install PHP 7.4 or above, and latest version of Composer.

$ sudo dnf -y update

2. Add remi repository :

# Fedora 33 
$ sudo dnf -y install https://rpms.remirepo.net/fedora/remi-release-33.rpm
# Fedora 32
$ sudo dnf -y install https://rpms.remirepo.net/fedora/remi-release-32.rpm
# Fedora 31
$ sudo dnf -y install https://rpms.remirepo.net/fedora/remi-release-31.rpm
# Fedora 30
$ sudo dnf -y install https://rpms.remirepo.net/fedora/remi-release-30.rpm
# Fedora 29
$ sudo dnf -y install https://rpms.remirepo.net/fedora/remi-release-29.rpm

3. Install PHP on Fedora

$ sudo dnf config-manager --set-enabled remi 
$ sudo dnf module reset php
$ sudo dnf module install php:remi-7.4

4. If the installation success, you can check PHP version that you installed before by using this command :

$ php -v
PHP 7.4.12 (cli) (built: Oct 27 2020 15:01:52) ( NTS ) Copyright (c) The PHP Group Zend Engine v3.4.0, Copyright (c) Zend Technologies

to install additional PHP Packages/Modules, you can use this command :

$ sudo yum install php-xxx

Now, let’s intsall Composer on the server :

$ curl -sS https://getcomposer.org/installer | php
$ sudo mv composer.phar /usr/local/bin/composer
$ sudo chmod +x /usr/local/bin/composer

To check Composer version that you installed, use this command :

$ composer -V
______
/ ____/___ ____ ___ ____ ____ ________ _____
/ / / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__ ) __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
/_/
Composer version 2.1.3 2021-06-09 16:31:20

Usage:
command [options] [arguments]

Options:
-h, --help Display this help message
-q, --quiet Do not output any message
-V, --version Display this application version
--ansi Force ANSI output
--no-ansi Disable ANSI output
-n, --no-interaction Do not ask any interactive question
--profile Display timing and memory usage information
--no-plugins Whether to disable plugins.
-d, --working-dir=WORKING-DIR If specified, use the given directory as working directory.
--no-cache Prevent use of the cache

STEP 2 (Install Nginx)

In order to run Laravel App in Production mode, we need Web Server. In this tutorial we use Nginx as Web Server. To install Nginx on your server, open the terminal using Putty or something and follow these steps :

$ sudo dnf install nginx

or using yum
$ yum -y install nginx

After Nginx installed, run Nginx using this command :

$ sudo systemctl start nginx

To stop Nginx :

$ sudo systemctl stop nginx

To restart Nginx :

$ sudo systemctl restart nginx

To auto run Nginx after server reboot :

$ sudo systemctl enable nginx

After nginx started, you can see the nginx test page by using this command :

$ curl http://localhost

STEP 3 (Clone project from the repository)

We have PHP and Nginx already installed on our server, now clone a Laravel project from repository like Github or something into our server. You have to clone it into root directory of Nginx, which is located in /usr/share/nginx/html/ :

$ cd /usr/share/nginx/html
$ sudo git clone http://sourcecontrolrepository.xxx/repositoryname.git

Install package/dependency from Laravel App using this command :

$ sudo composer update
$ sudo composer install

Make sure the folder permission of Laravel App directory has been set to 755

$ cd <laravel_project_root>
$ sudo chmod -R 755 vendor
$ sudo chmod -R 755 storage

Also make sure to setup Laravel Config :

$ cd <laravel_project_root>
$ sudo php artisan config:cache
$ sudo php artisan storage:link

Until this stage, actually your Laravel App is already running, you can access from this URL :

$ curl http://localhost/laravelapp/public/

But if you want to customize the path in order to make it prettier, we need to configure Nginx. So let’s move to the next step.

STEP 4 (Configure Nginx)

Go to the Nginx installation directory and edit the configuration file :

$ cd /etc/nginx
$ sudo nano nginx.conf

by default the configuration file will look like this :

you have to add several lines to configure the laravel path that you want :

After that, restart Nginx using this command :

$ sudo systemctl restart nginx

Now, you can test your access your application using the new path :

$ curl http://localhost/laravelapp

If you access it from the browser, it will look like this :

Now your Laravel application has been successfully running on Fedora Server. Enjoy!