How to deploy Laravel on Digital Ocean (LAMP)
Laravel is a PHP framework, great for create projects quickly, I will explain how to deploy a laravel app in a Digital Ocean Doplet, You need to have a Digital ocean account.
Create a Doplet
Digital Ocean has many options to create doplets preconfigured with a stack of tecnologies, we choose LAMP stack.
Choose size depents, if we go to create a test app or a production app (it increase cost).
Finally, we can choose a hostname and create a doplet.
Connect to Doplet
After Doplet installation, Digital Ocean sends an email with doplet access
Droplet Name: ************
IP Address: *************
Username: *******
Password: ********************
Next, we’ll use SSH to access the terminal of our digital ocean server (If you are in windows, you need to install a SSH client, I use MobaXterm), after to connect to the terminal, we have to change the password.
Configure Doplet
After connection, we’ll make sure eerything is updated
$ sudo apt-get update
$ sudo apt-get dist-upgrade
Once updated, we have to enable Apache mod_rewrite module
$ sudo a2enmod rewrite
Although our doplet has preconfigured LAMP stack, PhpMyAdmin isn’t installed, first, we need to install mysql we’ll use this comand to do it
$ mysql_secure_installation
Note: The password of mysql generally is located in /root/.digitalocean_password
$ nano /root/.digitalocean_password
Now we’ll to install PhpMyAdmin
$ sudo apt-get install phpmyadmin
Once finished installing PhpMyAdmin we need to add it into the bottom of to Apache’s config file
Include /etc/phpmyadmin/apache.conf
Note: apache2.conf file is in /etc/apache2/apache2.conf
$ sudo nano /etc/apache2/apache2.conf
Once that’s finished restart Apache
$ sudo service apache2 restart
Now our server ir ready to laravel, we have two ways, clone repo or create a Laravel project, in this tutorial we’ll clone repo from github, we need to install git and composer to do it
$ sudo apt-get install git
$sudo apt-get install composer
And now, we’ll clone the repo into /var/www/, once cloned we need to do composer installtion into the folder of our project
composer install
Once Laravel project is correctly installed we need to make sure that all of the permissions are set correctly, into our project folder and run the following commands:
$ sudo chown -R www-data: storage
$ sudo chmod -R 755 storage
Finally we need to point our server to the Laravel project public folder. We need to edit our Apache config
nano /etc/apache2/sites-enabled/000-default.conf
There are two lines in this config file that we need to updated
DocumentRoot /var/www
<Directory /var/www>
We need to change them to point the public folder in our project public folder
DocumentRoot /var/www/project/public
<Directory /var/www/project/public>
Also we need to add the following two lines inside of that same Directory tag
RewriteEngine On
RewriteBase /var/www/project/public
Once that’s finished and saved we need to restart Apache
$ sudo service apache2 restart
That’s all!, now we can go to the doplet public ip and see our Laravel project running.
Note: Make sure your .env is correctly configured.