DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 966,904 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Aaron Dunphy
Aaron Dunphy

Posted on • Updated on

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

Create a new user so we are not using root

adduser admin
usermod -aG sudo admin
su admin
Enter fullscreen mode Exit fullscreen mode

Install Apache

sudo apt update
sudo apt install apache2
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Then install

sudo apt update
sudo apt install php7.3
Enter fullscreen mode Exit fullscreen mode

Verify PHP was installed correctly

php -v
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

If you need any other extensions, you can install with

sudo apt install php7.3-<entension-name>
Enter fullscreen mode Exit fullscreen mode

Install MariaDB

sudo apt install mariadb-server mariadb-client
Enter fullscreen mode Exit fullscreen mode

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

sudo mysql_secure_installation
Enter fullscreen mode Exit fullscreen mode

You can now connect to MariaDB

mysql -u root -p
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

sudo nano /var/www/example.com/public/index.html
Enter fullscreen mode Exit fullscreen mode

Then insert and save

<html>
    <head>
        <title>Example.com!</title>
    </head>
    <body>
        <h1>Hello World</h1>
    </body>
</html>
Enter fullscreen mode Exit fullscreen mode

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

sudo nano /etc/apache2/sites-available/example.com.conf
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

Now let's enable the configuration file with Apache

sudo a2ensite example.com
Enter fullscreen mode Exit fullscreen mode

Restart Apache for the configuration file to kick in

sudo service apache2 restart
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Generate a ssh key for your server to communicate with GitHub

ssh-keygen
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Change directory to your domain

cd /var/www/example.com
Enter fullscreen mode Exit fullscreen mode

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

sudo rm -rf public/
Enter fullscreen mode Exit fullscreen mode

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/
Enter fullscreen mode Exit fullscreen mode

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

git clone git@github.com:<YourUserName>/<YourRepository>.git .
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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');"
Enter fullscreen mode Exit fullscreen mode

Then move it so it can be accessed globally

sudo mv composer.phar /usr/local/bin/composer
Enter fullscreen mode Exit fullscreen mode

Install dependencies from within your application folder

cd /var/www/example.com
composer install
Enter fullscreen mode Exit fullscreen mode

Install npm

sudo apt install npm
Enter fullscreen mode Exit fullscreen mode

Install dependencies and run tasks from within your application folder

cd /var/www/example.com
npm install
npm run dev
Enter fullscreen mode Exit fullscreen mode

Create database

Connect to MariaDB

mysql -u root -p
Enter fullscreen mode Exit fullscreen mode

Create database

create database my_db_name;
Enter fullscreen mode Exit fullscreen mode

Happy Deploying!

Top comments (0)

Update Your DEV Experience Level:

Settings

Go to your customization settings to nudge your home feed to show content more relevant to your developer experience level. πŸ›