Linux Server setup

Laravel

The PHP web framework Laravel is open source and follows the Model View Controller (MVC) paradigm.

We obtain the Laravel source code via Composer. As database we use MySQL.

The project directory in my example is /var/www/com.linuxserversetup.dev.

Next steps:


Install Laravel with Composer

We first switch to the web directory:


__$ cd /var/www/com.linuxserversetup.dev
	

And delete the content:


__$ rm -rf /var/www/com.linuxserversetup.dev/*
	

And create a Laravel project with create-project:


__$ composer create-project laravel/laravel ./
	

This created us Laravel project files and folders in /var/www/com.linuxserversetup.dev. Only these have been created by tom and thus are not in the www-data group. We change this with chgrp so that Apache also has access:


__$ sudo chgrp -R www-data /var/www/com.linuxserversetup.dev
	

Let's take a quick look inside:


__$ ls -la

Contents of /var/www/com.linuxserversetup.dev:


drwxrwxr-x 12 tom www-data   4096 Jan  4 21:54 .
drwxr-xr-x  8 tom tom        4096 Jan  4 19:38 ..
drwxrwxr-x  7 tom www-data   4096 Jan  4 10:07 app
-rwxr-xr-x  1 tom www-data   1686 Jan  4 10:07 artisan
drwxrwxr-x  3 tom www-data   4096 Jan  4 10:07 bootstrap
-rw-rw-r--  1 tom www-data   1745 Jan  4 10:07 composer.json
-rw-rw-r--  1 tom www-data 289317 Jan  4 21:54 composer.lock
drwxrwxr-x  2 tom www-data   4096 Jan  4 10:07 config
drwxrwxr-x  5 tom www-data   4096 Jan  4 10:07 database
-rw-rw-r--  1 tom www-data    258 Jan  4 10:07 .editorconfig
-rw-rw-r--  1 tom www-data    950 Jan  4 21:54 .env
-rw-rw-r--  1 tom www-data    899 Jan  4 10:07 .env.example
-rw-rw-r--  1 tom www-data    111 Jan  4 10:07 .gitattributes
-rw-rw-r--  1 tom www-data    207 Jan  4 10:07 .gitignore
-rw-rw-r--  1 tom www-data    473 Jan  4 10:07 package.json
-rw-rw-r--  1 tom www-data   1202 Jan  4 10:07 phpunit.xml
drwxrwxr-x  2 tom www-data   4096 Jan  4 10:07 public
-rw-rw-r--  1 tom www-data   4051 Jan  4 10:07 README.md
drwxrwxr-x  6 tom www-data   4096 Jan  4 10:07 resources
drwxrwxr-x  2 tom www-data   4096 Jan  4 10:07 routes
-rw-rw-r--  1 tom www-data    563 Jan  4 10:07 server.php
drwxrwxr-x  5 tom www-data   4096 Jan  4 10:07 storage
-rw-rw-r--  1 tom www-data    194 Jan  4 10:07 .styleci.yml
drwxrwxr-x  4 tom www-data   4096 Jan  4 10:07 tests
drwxrwxr-x 44 tom www-data   4096 Jan  4 21:54 vendor
-rw-rw-r--  1 tom www-data    559 Jan  4 10:07 webpack.mix.js

We are interested in the configuration file .envnext. These settings have priority over the files in the config folder.

Let's also take a look at the config folder:


__$ ls -la config

Contents of /var/www/com.linuxserversetup.dev/config:


drwxrwxr-x  2 tom www-data 4096 Jan  4 10:07 .
drwxrwxr-x 12 tom www-data 4096 Jan  4 21:54 ..
-rw-rw-r--  1 tom www-data 9418 Jan  4 10:07 app.php
-rw-rw-r--  1 tom www-data 3666 Jan  4 10:07 auth.php
-rw-rw-r--  1 tom www-data 1711 Jan  4 10:07 broadcasting.php
-rw-rw-r--  1 tom www-data 3274 Jan  4 10:07 cache.php
-rw-rw-r--  1 tom www-data  846 Jan  4 10:07 cors.php
-rw-rw-r--  1 tom www-data 5054 Jan  4 10:07 database.php
-rw-rw-r--  1 tom www-data 2282 Jan  4 10:07 filesystems.php
-rw-rw-r--  1 tom www-data 1571 Jan  4 10:07 hashing.php
-rw-rw-r--  1 tom www-data 3565 Jan  4 10:07 logging.php
-rw-rw-r--  1 tom www-data 3577 Jan  4 10:07 mail.php
-rw-rw-r--  1 tom www-data 2906 Jan  4 10:07 queue.php
-rw-rw-r--  1 tom www-data 2289 Jan  4 10:07 sanctum.php
-rw-rw-r--  1 tom www-data  950 Jan  4 10:07 services.php
-rw-rw-r--  1 tom www-data 7041 Jan  4 10:07 session.php
-rw-rw-r--  1 tom www-data 1053 Jan  4 10:07 view.php

An important command line tool for Laravel is artisan. This can be used to display the Laravel version, for example:


__$ php artisan -V

Output:


Laravel Framework 8.77.1


Edit .env file

We open the file:


__$ nano .env
 

And enter our connection data to MySQL in the database section:

Excerpt from /var/www/com.linuxserversetup.dev/.env


. . .
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laraveldb
DB_USERNAME=tom
DB_PASSWORD=tom123
. . .

Save and close (CTRL+s, CTRL+x).

When changing to the configuration, it must be renewed by artisan:


__$ php artisan config:clear
 

Using the Tinker Console we test if the database is connected. Let's open the console first:


__$ php artisan tinker
 

Psy Shell v0.10.12 (PHP 7.4.3 — cli) by Justin Hileman 
__tinker 

Trigger a neutral database function to test:


__tinker DB::connection()->getPdo();
 

The output should look something like this:


=> PDO {#3528
  inTransaction: false,
  attributes: {
    CASE: NATURAL,
    ERRMODE: EXCEPTION,
    AUTOCOMMIT: 1,
    PERSISTENT: false,
    DRIVER_NAME: "mysql",
    SERVER_INFO: "Uptime: 64331  Threads: 2  Questions: 43  Slow queries: 0  Opens: 201  Flush tables: 3  Open tables: 120  Queries per second avg: 0.000",
    ORACLE_NULLS: NATURAL,
    CLIENT_VERSION: "mysqlnd 7.4.3",
    SERVER_VERSION: "8.0.27-0ubuntu0.20.04.1",
    STATEMENT_CLASS: [
      "PDOStatement",
    ],
    EMULATE_PREPARES: 0,
    CONNECTION_STATUS: "127.0.0.1 via TCP/IP",
    DEFAULT_FETCH_MODE: BOTH,
  },
}

Tinker can be terminated with q:


__tinker q
 

Another important note: It is recommended not to include the .env file in Git repositories, as sensitive data such as passwords are stored here.


Configure Nginx for Laravel

We had already created the Nginx server block for the dev.linuxserversetup.com subdomain in the chapter Subdomain Server Block (dev).

Nginx is responsible at this point for enforcing HTTPS and encrypting the connection for the subdomain. Now we need to modify the server block so that communication via the domain is routed to and from the Laravel application.


__$ sudo nano /etc/nginx/sites-available/com.linuxserversetup.dev.conf
 

The Nginx configuration should eventually look like this:

/etc/nginx/sites-available/com.linuxserversetup.dev.conf


# force https
server {
  listen      80;
  server_name dev.linuxserversetup.com;
  return      301 https://$server_name$request_uri;
}

# main block
server {
  listen      443 ssl http2;
  listen      [::]:443 ssl http2;
  server_name dev.linuxserversetup.com;

  root        /var/www/com.linuxserversetup.dev/public;
  index       index.php index.htm;

  location / {
    proxy_pass http://127.0.0.1:3600;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $host;
  }

  location ~* ^.*\.php$ {
    proxy_pass http://127.0.0.1:3600;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $host;
  }

  location ~ /\.(htaccess|git)(/.*|$) {
    deny all;
  }

  ssl_certificate /etc/letsencrypt/live/dev.linuxserversetup.com/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/dev.linuxserversetup.com/privkey.pem;
  ssl_trusted_certificate /etc/letsencrypt/live/dev.linuxserversetup.com/chain.pem;

  # dhparam
  ssl_dhparam /etc/ssl/certs/dhparam.pem;

  # HSTS
  add_header Strict-Transport-Security "max-age=31536000";
}

In order for the configuration to be applied by Nginx, we check the new settings and restart the service:


__$ sudo nginx -t
__$ sudo systemctl restart nginx
 

If the Nginx configuration is ok, this feedback should come:


nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
 

Launch and test Laravel app

To test the Laravel application, we just need to access the domain dev.linuxserversetup.com through a browser.

If everything is configured correctly, then the Laravel web framework start page should appear.