Installing Laravel [php8.1 fpm, nginx, ubuntu 22.04]

On local machine

I prefer to install it via composer so here it goes:

$ composer create-project laravel/laravel example-app

$ cd example-app

$ php artisan serve

Above is for one project only. If you want to install the laravel installer globally, then run the following:

$ composer global require laravel/installer

$ laravel new example-app

$ php artisan serve

This will create the basic structure of the laravel project. if there's no vendor directory, then run composer install in order to fetch the vendor files.

Now you should create the repo in github so that the local project could be uploaded to it.

  • Create a new repository (DO NOT initiate it with readme or any other files in order to avoid errors later on).
  • git clone <URL> and it will clone the repo. (Change the visibility of the project to private if required)
  • In your local computer (where the local project resides), go to the terminal and go to the working directory.
  • Initialize the local directory as git repository vide the following command
$ git init -b main

Add the files in your repository,

$ git commit -m "First commit"
# Commits the tracked changes and prepares them to be pushed to a remote repository. 
# To remove this commit and modify the file, use 'git reset --soft HEAD~1' and commit and add the file again.

Copy the url of the git repo in github. In terminal, add the following to add the remote repository.

$ git remote add origin  <REMOTE_URL>
# Sets the new remote
$ git remote -v
# Verifies the new remote URL

Push the changes.

$ git push origin main
# Pushes the changes in your local repository up to the remote repository you specified as the origin

This will push the local project in to the github repository.

Note: -b unknown swtich error

If you get any errors regarding -b unknown switch, then you should update the git in your machine as -b command is available only after git 2.28 >and later on. In order to update git, do the following:

$ sudo add-apt-repository -y ppa:git-core/ppa
$ sudo apt update
$ sudo apt install git -y

This will update the git in your machine and you will resolve the unknown switch error.

You may get the following error upon pushing repo to github upon second push.

No upstream branch error

fatal: The current branch main has no upstream branch. To push the current branch and set the remote as upstream, use

git push --set-upstream origin main

If you get this error, then run,

$ git push --set-upstream origin main

On server

Change the visibility of the repo to public so that it can be cloned without much hassle.

git clone <URL> and it will clone the repo. (Change the visibility of the project to private if required)

Setup the ssh deploy keys as written here for future git pull.

run composer install in order to complete the setup.

Required Extensions

There are two specific extensions which are required to be present in the server in order to run composer installer.

  • mbstring
  • phpunit

If any errors are thrown above, then you should install these extensions via the following.

$ sudo apt update
$ sudo apt install php-mbstring phpunit

This will install the laravel but the setup is still not finished yet. In order to make life simpler, create a .env file in the root directory of the laravel installation.

Setting up permissions


# go to the laravel directory
$ cd /var/www/html/laravel
$ sudo chown -R $USER:www-data .
$ sudo find . -type f -exec chmod 664 {} \;
$ sudo find . -type d -exec chmod 775 {} \;
$ sudo chgrp -R www-data storage bootstrap/cache
$ sudo chmod -R ug+rwx storage bootstrap/cache

Above settings should be able to give you the default welcome page of Laravel without any issues.

Setting up environment

After setting up the permissions, it is time to setting up the environment


$ touch .env

In the .env file, write the following:

APP_DEBUG=true
#I am changing it true in production server as other issues may creep in. Once, everything is ok, change it to false.
APP_KEY=base64:/<your base64 string>

You should generate the key by php artisan key:generate

WARNING

Extension not found

If you are getting extension not found/missing errors, then you should install via the following (Supposing you have php8.1 installed):

sudo apt install php8.1-extension-name

#e.g. 
sudo apt install php8.1-gd
sudo apt install php8.1-mbstring

etc.

Setting up nginx as webserver for laravel framework


Edit the following in nginx (/etc/nginx/sites-enabled/default):

```nginx
server {

    server_name example.xopun.com;
    listen 80;
    root /var/www/example.xopun.com;

    index index.php;

    location / {
            try_files $uri $uri/ /index.php?$query_string;
    }


    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php8.1-fpm.sock;
    }

Installing Redis for laravel

sudo apt update
sudo apt install redis-server

Make the following changes to /etc/redis/redis.conf via executing the following command:

sudo nano /etc/redis/redis,conf

change the supervised directive from "no" to "systemd"

. . .

# If you run Redis from upstart or systemd, Redis can interact with your
# supervision tree. Options:
#   supervised no      - no supervision interaction
#   supervised upstart - signal upstart by putting Redis into SIGSTOP mode
#   supervised systemd - signal systemd by writing READY=1 to $NOTIFY_SOCKET
#   supervised auto    - detect upstart or systemd method based on
#                        UPSTART_JOB or NOTIFY_SOCKET environment variables
# Note: these supervision methods only signal "process is ready."
#       They do not enable continuous liveness pings back to your supervisor.
supervised systemd

. . .
sudo systemctl restart redis.service

Testing Redis

sudo systemctl status redis

You should see similar to following:

ubuntu:~$ sudo systemctl status redis
● redis-server.service - Advanced key-value store
     Loaded: loaded (/lib/systemd/system/redis-server.service; enabled; vendor preset: enabled)
     Active: active (running) since Thu 2022-08-25 07:46:38 IST; 46min ago
       Docs: http://redis.io/documentation,
             man:redis-server(1)
   Main PID: 5530 (redis-server)
     Status: "Ready to accept connections"
      Tasks: 5 (limit: 9305)
     Memory: 2.6M
        CPU: 3.411s
     CGroup: /system.slice/redis-server.service
             └─5530 "/usr/bin/redis-server 127.0.0.1:6379" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""

Aug 25 07:46:38 15isk systemd[1]: Starting Advanced key-value store...
Aug 25 07:46:38 15isk systemd[1]: Started Advanced key-value store.

To test that Redis is functioning correctly, connect to the server using redis-cli, Redis’s command-line client:

$ redis-cli

In the prompt that follows, test connectivity with the ping command:

127.0.0.1:6379> ping

Output

PONG

This output confirms that the server connection is still alive. Next, check that you’re able to set keys by running:

127.0.0.1:6379>set test "It's working!"

Output:

OK

Retrieve the value by typing:

127.0.0.1:6379>get test

Assuming everything is working, you will be able to retrieve the value you stored:

Output:

It's working!

Install the redis extension

sudo apt install php8.1-redis

Install predis/predis

Install the redis package for laravel via composer

composer require predis/predis

In config/app.php

'aliases' => Facade::defaultAliases()->merge([
    'Redis' => Illuminate\Support\Facades\Redis::class,
])->toArray(),

Redis should start to work without any issues.

Last Updated: