Creating and installing a blank Laravel app on an Ubuntu 22.04 Vultr instance to be snapshotted for rapid redeployment.
This includes Apache, Composer and PHP 8.1 installations.
Current promotion free $100 credit on new Vultr accounts here!
Choose your starting base instance, I am choosing a $6 p/m AMD high performance compute with 25GB. That means the snapshot will be compatible with this plan as you cannot deploy larger snapshots back onto smaller plans.
It is recommended to setup using an SSH key, this makes it fast to connect to future instances as you “don’t need to know the password”.
Start by updating the package list:
sudo apt update
Install Apache
Install apache2 HTTP webserver:
sudo apt install apache2
Most likely UFW (firewall) will be running as default, you will need to allow Apache through with:
sudo ufw allow 'Apache'
Install PHP 8.1
For PHP 8.1 you may need to install the independent software sources first, this can be done with
sudo apt install software-properties-common
This is needed for the latest PHP versions repository
sudo add-apt-repository ppa:ondrej/php
To install PHP 8.1 and the PHP 8.1 module for Apache
sudo apt install php8.1 libapache2-mod-php8.1
Installing PHP 8.1 extensions is done in the following format:
sudo apt install php8.1-[extension_name]
An example with a few common extensions
sudo apt install php8.1-{curl,mysql,xml}
Restart Apache
sudo systemctl restart apache2
Install Composer
Composer is a dependency or package manager for PHP, it makes installing and updating packages to your project easy.
Install the PHP comandline interface
sudo apt install php-cli unzip
Go to your home directory and download the composer installer
cd ~ curl -sS https://getcomposer.org/installer -o /tmp/composer-setup.php
Install composer globally
sudo php /tmp/composer-setup.php --install-dir=/usr/local/bin --filename=composer
The system-wide command name for Composer will be composer
.
To test the installation was successful, run:
composer
and you will see the Composer version and usage options page
Install Laravel
Navigate to
cd /var/www
Create a new Laravel project with Composer
composer create-project laravel/laravel myapp
this puts the Laravel project in /var/www/myapp
To verify a successful Laravel install go into the directory and run PHP artisan:
cd myapp php artisan
Similar to Composer you will get an output with the Laravel version and usage commands.
Permissions
Add some permissions to the Laravel project directory so the webserver (Apache) can access it
sudo chown -R www-data:www-data /var/www/myapp
and
sudo find /var/www/myapp/ -type d -exec chmod 755 {} \;
Virtual Hosts
Create a new virtual host for the Larvel app
sudo nano /etc/apache2/sites-available/myapp.conf
Edit and past in the following:
<VirtualHost *:80> ServerAdmin contact@domain.com DocumentRoot /var/www/myapp/public ServerName DOMAINIP.com ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined <Directory /var/www/myapp/> Options +FollowSymlinks AllowOverride All Require all granted </Directory> </VirtualHost>
You need to replace DOMAINIP.com
with the domain you are using or you can use the instance IP address as a placeholder.
Press ctrl + x
then y
to save.
Apply this configuration file
sudo a2ensite myapp.conf
Make sure mod_rewrite is enabled
sudo a2enmod rewrite
Finally restart Apache
sudo systemctl restart apache2
When you navigate to http://domain.tld or http://ipaddress you will see the Laravel welcome page:
Creating a snapshot
Taking an instance snapshot on Vultr means you can skip all of the previous steps and rapidly redeploy this Laravel setup to use at other locations or at another time.
The only thing you would need changing is the ServerName in the myapp.conf and DNS changes externally for your domain name.
To take a snapshot on your instance page click the Snapshots tab, and create a label name such as “Base Laravel app”. Finally click “Take Snapshot”.
To use this snapshot, instead of choosing an Operating System click the Snapshot tab and choose “Base Laravel app”.