Laravel is an open-source PHP framework designed to make developing web apps mush easier and faster through built-in features. It is based on the MVC design pattern.
In this article, I will show you how to deploy a Laravel application to Azure App Services. The Azure web server is NGINX running on Linux version "Debian GNU/Linux 10 (buster)".
Assumptions
The following is assumed:
- You have Git, PHP and Composer installed on your computer.
- You have an Azure subscription
- You have a GitHub account
Getting Started
We will start our journey with a very simple Laravel application that lists some data from a SQLite database. Go into a working directory on your computer and run the following command from a terminal window to clone a GitHub repo:
git clone https://github.com/medhatelmasry/laravel-azure.git
It is a good idea to delete the .git folder on your computer.
Once the application is cloned, change directory to the cloned app and start your app as shown below:
cd laravel-azurecomposer installphp artisan serve --port=8888
The web server will start on your computer listening on port 8888.
Starting Laravel development server: http://127.0.0.1:8888[Sat Mar 26 15:36:29 2022] PHP 8.1.2 Development Server (http://127.0.0.1:8888) started
Point your browser to http://localhost:8888/ and you will see the following page:
Disclaimer: It is normal practice that the .env file is excluded from being pushed to source control. If you look at the .gitignore file, the line pertaining to .env is commented out because there is really no confidential information in this file.
Create a repository in your GitHub account and push the source code to it.
Create Azure App Service
Login to your Azure account by visiting https://portal.azure.com. Enter "app services" in the filter field, then click on "App Services".
Click on "+ Create" on the top left-side.
Give your app a suitable host name that is unique.
Of course, we want to deploy our Laravel PHP site from our GitHub repo. An easy way to achieve this is to use Azure's "Deployment Center". Click on "Deployment Center" in the left-side navigation.
The workflow files were automatically generated and placed a .yml file in the .github/workflows folder with your source code.
At this point, it is worth going back to the code on your computer and doing a pull of your code so that you get a copy of the .yml file that was added to your source code.
Configuring app on Azure
Back on the Azure portal, if you refresh the default page of our web app, you will experience a "403 Forbidden" message, which typically means that the root directory has no welcome page.
This is understandable because the main index.php page in Laravel resides in the /public folder. THis means that we need to do some configuration work on Azure.
In Azure, enter the term advanced in the search input field then click on "Advanced Tools".
A Linux terminal window is open.
If you are interested to know what version of Linux this is, enter the following terminal command:
cat /etc/os-release
The value beside PRETTY_NAME is the Linux distribution and version.
PRETTY_NAME="Debian GNU/Linux 10 (buster)"
NAME="Debian GNU/Linux"
VERSION_ID="10"
VERSION="10 (buster)"
VERSION_CODENAME=buster
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"
We will make a copy of the existing nginx configuration and place the file inside the /home/site directory with the following command:
cp /etc/nginx/sites-available/default /home/site/default
Once copied, edit the /home/site/default file with nano or vi. I will use nano.
nano /home/site/default
Make the following changes:
From | To | Around Line # |
---|---|---|
root /home/site/wwwroot | root /home/site/wwwroot/public | 6 |
location / { index index.php index.html index.htm hostingstart.html; } |
location / {
index index.php index.html index.htm hostingstart.html;
try_files $uri $uri/ /index.php?$query_string;
}
|
10 |
We need to create a bash script file that overrides the existing default file with our customized version, then restart the server. Change directory to the site folder.
cd site
Inside the site directory, using vi or nano, create a file named startup.sh. To create a file with nano, type "nano startup.sh". Otherwise, to create a file with vi, type "vi startup.sh". Add the following content to startup.sh:
#!/bin/bashcp /home/site/default /etc/nginx/sites-available/defaultservice nginx reload
We will make our bash script file executable with:
chmod u+x startup.sh
While in the terminal window on Azure, let's visit the folder that contains our Laravel application. Do this by going to the wwwroot folder.
cd wwwrootls -a
This reveals the existence of all the files that we had on GitHub plus files in the vendor folder.
Navigate back to your App Service via the Azure Portal. Select Configuration in the Settings section.
Click on the "General Settings" tab, enter "/home/site/startup.sh" for the "Startup Command", then click on Save.
We have successfully deployed a database driven Laravel PHP application to Azure App Services through GitHub. I hope you use the principles you learned in this tutorial to deploy much more interesting Laravel applications.
No comments:
Post a Comment