Deploying a Laravel Real world application using Debian 11 virtual machine instance with Amazon Web Services (AWS)
- Demo Project: https://github.com/f1amy/laravel-realworld-example-app.git
- Setup Debain 11 on a virtual machine instance with a cloud provider
- Setup Apache2 with every dependency the application needs to run
- clone the project with Git and deploy with apache2
- Setup MySQL with credentials and a database for the application to use
- Configure a subdomain if you have a domain name to point to the Vm instance or speak to an instructor for futher guide
- You have complete the project if you are able to view the application according to the specifications in the project from your Host browser
We will be using Debain 11 virtual machine instance with Amazon Web Services (AWS) as our Virtual Cloud provider.
NOTE: Your Amazon Elastic Compute Cloud (Amazon EC2) instance Inbound Rules should be configured to allow port 22 for SSH, port 80 for http and port 443 for https.
$ sudo apt update && sudo apt upgrade -y
$ sudo apt install git -y
After installation is completed, the below command will prompt the installed version of Git in the AWS server. Hence, the installation is completed
$ git --version
$ sudo apt install apache2
$ sudo apache2 -v
$ sudo systemctl enable apache2
$ sudo systemctl start apache2
$ sudo systemctl start apache2
$ cd /var/www/html
$ sudo git clone https://github.com/f1amy/laravel-realworld-example-app.git
$ cd /var/www/html/laravel-realworld-example-app
The packages to install PHP 8.1 on Debian 11 are not available in the default base repository, hence we have to add the SURY repository manually. The below given is a single command, COPY THE ENTIRE COMMAND AT ONCE and paste it in your terminal. And hit the Enter key
$ echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main"\ | sudo tee /etc/apt/sources.list.d/sury-php.list
$ sudo apt install gnupg gnupg2
The system needs to verify the packages we receive to install PHP 8.0 from the Sury repository, to confirm they have not been altered by anyone
$ wget -qO - https://packages.sury.org/php/apt.gpg | sudo apt-key add -
$ sudo apt update && sudo apt upgrade -y
We can go for the installation of php and the required extensions as per your web application requirements.
$ sudo apt-get install php8.1 libapache2-mod-php php8.1-dev php8.1-zip php8.1-curl php8.1-mbstring php8.1-mysql php8.1-gd php8.1-xml
$ sudo gpg --keyserver pgpkeys.mit.edu --recv-key B188E2B695BD4743
$ sudo wget -qO - https://packages.sury.org/php/apt.gpg | sudo apt-key add -
$ sudo apt-get install php8.1 libapache2-mod-php php8.1-dev php8.1-zip php8.1-curl php8.1-mbstring php8.1-mysql php8.1-gd php8.1-xml
-
php8.1-cli - command interpreter, useful for testing PHP scripts from a shell or performing general shell scripting tasks
-
php8.1-common - documentation, examples, and common modules for PHP
-
php8.1-mysql - for working with MySQL databases
-
php8.1-zip - for working with compressed files
-
php8.1-gd - for working with images
-
php8.1-mbstring - used to manage non-ASCII strings
-
php8.1-curl - lets you make HTTP requests in PHP
-
php8.1-xml - for working with XML data
-
php8.1-bcmath - used when working with precision floats
$ php -v
Configuration of MySQL repsotiory on Debian and on its Offical Community Download Page and there you will get the latest Apt repsotiory in the deb binary form to download. You can also use the given commands instead
$ sudo apt install wget -y
$ wget https://dev.mysql.com/get/mysql-apt-config_0.8.20-1_all.deb
$ sudo apt install ./mysql-apt-config_*_all.deb
$ sudo apt update && sudo apt upgrade
if error occurs... Confirm your PUBLIC_KEY from your ERROR MESSEGAE ...and run the following commands
$ sudo apt-key adv --keyserver pgp.mit.edu --recv-keys 467B942D3A79BD29
$ sudo apt update && sudo apt upgrade -y
You will get option to select for what you want to add via the repository. Use the Arrow key, select MySQL Server & Cluster , then press the TAB button to select OK and hit then the Enter key
Select the available version of MySQL Server, here in our case it was myslq-8.0. After that use the TAB to select OK and hit the Enter key
During the installation process, it will prompt us to SET A PASSWORD FOR THE ROOT USER, make sure to enter a strong password and Hit the ENTER KEY
Next it the again prompt us to re-enter our password, after this has been done, Hit the ENTER KEY again to exit
$ sudo apt install mysql-server
$ mysql --version
$ sudo systemctl enable --now mysql
$ sudo systemctl status mysql
$ sudo mysql_secure_installation
Depending on your version it might prompt you to install the VALIDATE PASSWORD plugin. This is not necessary since we will have control over making new users and we will make sure the passwords are secure enough, so we will skip over this setting. (Type ‘N’).
Next, it will prompt us if we want to change the existing password for the root user. If you had set a secure password in the last step then you can also skip this question by pressing any key (other than ‘Y’). If you left the password blank or set up a random password (like ‘password’ or ‘1234’) then go ahead and change your password now. You can generate a password here if you used a password that is not strong.
Another prompt will asks to Remove the test database and access it. This is a good idea to do, so type Y for this.
$ sudo nano /etc/apache2/sites-available/laravel.conf
<VirtualHost *:80>
ServerAdmin [email protected]
ServerName 44.212.4.90
DocumentRoot /var/www/html/laravel-realworld-example-app/public
<Directory /var/www/html/laravel-realworld-example-app/public>
Options +FollowSymlinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
We can now save and close the file and then enable the Apache rewrite module and activate the Laravel virtual host
$ sudo a2enmod rewrite
$ sudo a2ensite laravel.conf
$ sudo systemctl restart apache2
In your terminal, open the php.ini file with nano
$ sudo nano /etc/php/8.1/apache2/php.ini
The line we need to edit is cgi.fix_pathinfo=0 so you can either search for it manually, or you can search for it using Ctrl+W
Press Ctrl+W and now type in cgi.fix_pathinfo and click enter. This will take you to the right line. You will see a semicolon on the left of this line. Delete the semicolon and then change the value from 1 to a 0 and save the file.
Restart the Apache service to apply changes
$ sudo systemctl restart apache2
Composer is a PHP dependency manager that manages the dependencies and libraries that PHP applications require. We will need it to install the Laravel dependencies and packages.
$ curl -sS https://getcomposer.org/installer -o /tmp/composer-setup.php
To facilitate the verification step, you can use the following command to programmatically obtain the latest hash from the Composer page and store it in a shell variable
$ HASH=`curl -sS https://composer.github.io/installer.sig`
$ echo $HASH
Now execute the following PHP code, as provided in the Composer download page, to verify that the installation script is safe to run
$ php -r "if (hash_file('SHA384', '/tmp/composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
If the output says Installer corrupt, you’ll need to download the installation script again and double check that you’re using the correct hash. Then, repeat the verification process. When you have a verified installer, you can continue.
To install composer globally, use the following command which will download and install Composer as a system-wide command named composer, under /usr/local/bin
$ sudo php /tmp/composer-setup.php --install-dir=/usr/local/bin --filename=composer
$ composer
$ sudo composer install --no-dev
In order to run, Apache needs certain permissions over the Laravel directory we made. We must first give our web group control of the Laravel-realworld-example-app directory
$ sudo chown -R :www-data /var/www/html/laravel-realworld-example-app
$ sudo chmod -R 775 /var/www/html/laravel-realworld-example-app
$ sudo chmod -R 775 /var/www/html/laravel-realworld-example-app/storage
$ sudo chmod -R 775 /var/www/html/laravel-realworld-example-app/bootstrap/cache
$ sudo cp .env.example .env
$ sudo php artisan key:generate
We can now create a database to store our application data in it. We can set up the database that our application will be using in production.
$ mysql -u root -p
CREATE DATABASE laraveldatabase;
We can start by adding the database credentials to the .env file we had generated earlier. This file contains all the application variables that need to be kept secret
NOTE::: APP_KEY=base64 should be left untouched!
$ sudo nano .env
$ sudo nano routes/web.php
$ sudo php artisan config:cache
$ sudo a2dissite 000-default.conf
$ sudo a2ensite laravel.conf
$ sudo systemctl status apache2
$ sudo php artisan migrate
At this point, we can now access our laravel application through the URL https://AWS-public-ip
- Website - Bukola Testimony
- Twitter - @BukolaTestimony