How to Deploy a Laravel Application on a Fresh Install of Debian 10

14 July 2019

Create a new user so we are not using root

adduser admin
usermod -aG sudo admin
su admin

Install Apache

sudo apt update
sudo apt install apache2

Install PHP7.3

Add PHP7.3 repository

sudo apt install lsb-release apt-transport-https ca-certificates
sudo wget -O /etc/apt/trusted.gpg.d/php.gpg
sudo echo "deb $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/php7.3.list

Then install

sudo apt update
sudo apt install php7.3

Verify PHP was installed correctly

php -v

Install common PHP extensions

sudo apt install php7.3-cli php7.3-fpm php7.3-json php7.3-pdo php7.3-mysql php7.3-zip php7.3-gd  php7.3-mbstring php7.3-curl php7.3-xml php7.3-bcmath php7.3-json php7.3-zip

If you need any other extensions, you can install with

sudo apt install php7.3-<entension-name>

Install MariaDB

sudo apt install mariadb-server mariadb-client

Then secure the database, set a strong password, remove anonymous user, disable remote login for root and remove test database

sudo mysql_secure_installation

You can now connect to MariaDB

mysql -u root -p

Set up Virtual Host

Create the directory of where your application will live (replace "" with your domain)

sudo mkdir -p /var/www/

Create a dummy holding page so we can test the virtual host configuration has worked

sudo nano /var/www/

Then insert and save

        <h1>Hello World</h1>

Create a name based virtual host configuration file to allow Apache to serve it

sudo nano /etc/apache2/sites-available/

Then insert and save (replace all refences of with your own domain)

<VirtualHost *:80>

DocumentRoot /var/www/

<Directory /var/www/>
    AllowOverride all
    Require all granted

ErrorLog ${APACHE_LOG_DIR}/example.com_error.log
CustomLog ${APACHE_LOG_DIR}/example.com_access.log combined


Now let's enable the configuration file with Apache

sudo a2ensite

Restart Apache for the configuration file to kick in

sudo service apache2 restart

As long as your domain is pointing to your server's IP address, you should now be able to see the dummy html file we added earlier by going to your domain e.g -

Clone your Laravel app with Git

Install Git

sudo apt install git

Generate a ssh key for your server to communicate with GitHub


Copy your server's SSH public key and add it to your GitHub's repository deploy keys which can be found under the repository settings (I would suggest not enabling write access)

cat ~/.ssh/

Change directory to your domain

cd /var/www/

Delete the public directory from one of the previous instructions above so it is completely empty

sudo rm -rf public/

Update the directory so it is owned by the admin user but accessible by apache

sudo chown -R server-user:www-data /var/www/

Clone your repository with ssh (you can get this from your repository on GitHub)

git clone<YourUserName>/<YourRepository>.git .

Update file/directory ownerships again after cloning and also ensure apache can read/write to the storage and cache.

sudo chown -R server-user:www-data /var/www/
find /var/www/ -type f -exec chmod 664 {} \;    
find /var/www/ -type d -exec chmod 775 {} \;
chgrp -R www-data storage bootstrap/cache
chmod -R ug+rwx storage bootstrap/cache

Install composer

Following their official docs

php -r "copy('', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === '48e3236262b34d30969dca3c37281b3b4bbe3221bda826ac6a9a62d6444cdb0dcd0615698a5cbe587c3f0fe57a54d8f5') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"

Then move it so it can be accessed globally

sudo mv composer.phar /usr/local/bin/composer

Install dependencies from within your application folder

cd /var/www/
composer install

Install npm

sudo apt install npm

Install dependencies and run tasks from within your application folder

cd /var/www/
npm install
npm run dev

Create database

Connect to MariaDB

mysql -u root -p

Create database

create database my_db_name;

Happy Deploying!

© Aaron Dunphy 2021

Built with Gatsby & ☕️