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

Aaron Dunphy • June 14, 2019

sysadmin laravel debian

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 https://packages.sury.org/php/apt.gpg
sudo echo "deb https://packages.sury.org/php/ $(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 "example.com" with your domain)

sudo mkdir -p /var/www/example.com/public

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

sudo nano /var/www/example.com/public/index.html

Then insert and save

<html>
    <head>
        <title>Example.com!</title>
    </head>
    <body>
        <h1>Hello World</h1>
    </body>
</html>

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

sudo nano /etc/apache2/sites-available/example.com.conf

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

<VirtualHost *:80>

ServerAdmin example@me.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com/public

<Directory /var/www/example.com>
    AllowOverride all
    Require all granted
</Directory>

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

</VirtualHost>

Now let's enable the configuration file with Apache

sudo a2ensite example.com

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 - http://example.com

Clone your Laravel app with Git

Install Git

sudo apt install git

Generate a ssh key for your server to communicate with GitHub

ssh-keygen

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/id_rsa.pub

Change directory to your domain

cd /var/www/example.com

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/example.com/

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

git clone git@github.com:<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/example.com/
find /var/www/example.com/ -type f -exec chmod 664 {} \;    
find /var/www/example.com/ -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('https://getcomposer.org/installer', '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/example.com
composer install

Install npm

sudo apt install npm

Install dependencies and run tasks from within your application folder

cd /var/www/example.com
npm install
npm run dev

Create database

Connect to MariaDB

mysql -u root -p

Create database

create database my_db_name;

Happy Deploying!

Want posts straight to your inbox?