Skip to content

Instantly share code, notes, and snippets.

@mikeqci
Last active November 2, 2022 16:08
Show Gist options
  • Save mikeqci/0f0464c22ed85af8c267799835941f12 to your computer and use it in GitHub Desktop.
Save mikeqci/0f0464c22ed85af8c267799835941f12 to your computer and use it in GitHub Desktop.
Deploy Laravel 8 on Azure App Service and Connect it to a SQL Server Database
# Step 1: create an Azure App Service resource
# Step 2: in Azure, go to Configuration, add your .env params to the Application Settings
# PRO TIP: The format for Advanced Edit (which no one f***ing tells you) is
# [
# {
# "name": "APP_DEBUG",
# "value": "true",
# "slotSetting": false
# },
# {
# "name": "APP_ENV",
# "value": "production",
# "slotSetting": false
# }
# ... etc
# ]
# Save changes
# Step 3: the next tab on this page is General Settings (Configuration > General Settings)
# Stack: PHP
# Major Version: PHP 8
# Minor Version: PHP 8.1 (or whatever you want, I'm not the boss of you)
# Startup Command: /home/site/wwwroot/azure/azure-config.sh
# NOTE: we will create the above file in a bit
# Everything else: default is fine I guess, I didn't change any thing after that.
# Save
# Step 4: Deployment Center
# Source: Github
# (add your github account, the right branch, etc - this is up to you. I don't recommend master because you'll commit to master a lot, but whatever you want)
# IMPORTANT: Build Service Provider: App Service Build Service (this is the little link right under the first Github dropdown)
# IF YOU LEAVE IT AS GITHUB PIPELINES (or whatever) THE APP WILL TAKE LIKE 20+ MIN TO DEPLOY
# SAVE
# Step 5: Create a folder called azure (at the root of your project.
# Create a file called azure-config.sh in that azure folder
# If you change the name or the location, go update the Startup Command field above in Azure so it can find it
# NOTE: don't tack radom commands onto the Startup Command field in Azure because it expects a script name and will fail quietly
################################## FILE START | azure/azure-config.sh ##################################
# Azure > My App Service > Configuration > General Settings > Startup Command:
# /home/site/wwwroot/production-install.sh
# this gets fired each time it builds a new server (most code deploys)
# copy nginx config to /etc/nginx/sites-enabled
su -;
cp /home/site/wwwroot/azure/azure-nginx /etc/nginx/sites-enabled/default;
#install sqlsrv pdo
#DEBIAN required stuff for sqlsrv pdo
curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
curl https://packages.microsoft.com/config/debian/11/prod.list > /etc/apt/sources.list.d/mssql-release.list
apt-get update
ACCEPT_EULA=Y apt-get install -y msodbcsql18
# optional: for bcp and sqlcmd
ACCEPT_EULA=Y apt-get install -y mssql-tools18
echo 'export PATH="$PATH:/opt/mssql-tools18/bin"' >> ~/.bashrc
source ~/.bashrc
apt-get install -y unixodbc-dev
apt-get install -y libgssapi-krb5-2
# IF YOU DON'T DO su -
# YOU WILL GET SOME STUPID DERROR
su -
pecl install pdo_sqlsrv
pecl install sqlsrv
cp /home/site/wwwroot/azure/custom_php.ini /usr/local/etc/php/conf.d/;
# install crontab and establish the cron job that runs laravels built in job manager
apt-get -y install cron;
(crontab -l; echo '* * * * * bash -lc "(/usr/local/bin/php /home/site/wwwroot/artisan schedule:run >> /dev/cron.log)"') | crontab -;
service cron start;
service nginx reload;
service nginx restart;
################################## FILE END | azure/azure-config.sh ##################################
# STEP 6: Create a file called custom_php.ini in that azure folder with the following content
################################## FILE START | azure/custom_php.ini ##################################
extension=pdo_sqlsrv.so
extension=sqlsrv.so
################################## FILE END | azure/custom_php.ini ##################################
# STEP 7: Create a file called azure-nginx in that azure folder with the following content
################################## FILE START | azure/azure-nginx ##################################
# this gets copied to /etc/nginx/sites-enabled
# using Azure > My Project > Configuration > General Settings > Startup Command > /home/site/wwwroot/azure/azure-config.sh
server {
#proxy_cache cache;
#proxy_cache_valid 200 1s;
listen 8080;
listen [::]:8080;
root /home/site/wwwroot/public;
index index.php index.html index.htm;
server_name example.com www.example.com;
port_in_redirect off;
error_log /home/site/nginx.log debug;
location @rewrite {
rewrite ^/(.*)$ /index.php?_url=/$1;
}
location / {
try_files $uri $uri/ @rewrite;
}
# redirect server error pages to the static page /50x.html
#
#error_page 500 502 503 504 /50x.html;
#location = /50x.html {
# root /html/;
#}
# Disable .git directory
location ~ /\.git {
deny all;
access_log off;
log_not_found off;
}
# Add locations of phpmyadmin here.
location ~ [^/]\.php(/|$) {
fastcgi_split_path_info ^(.+?\.php)(|/.*)$;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param HTTP_PROXY "";
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param QUERY_STRING $query_string;
fastcgi_intercept_errors on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 3600;
fastcgi_read_timeout 3600;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
}
}
################################## FILE END | azure/azure-nginx ##################################
# STEP 8: commit these files to your git repo, and merge them into whichever branch you tagged in Deployment Center
# Every time you update that branch in Git, Azure will automatically deploy a new verison, which you can watch in
# Azure > My Project > Deployment Center > Logs
# To deploy for the first time, go to Deployment Center and hit Sync
# Add your domain under Custom Domains or your ssl cert (and probably forms) wont work
# Debugging: If it still shows the placeholder site:
# you might need to go to the My Project > SSH menu, ssh into the box, and run this install the first time by hand
#
# SSH into the box, and run:
# su -; /home/site/wwwroot/azure/azure-config.sh;
# If you can't connect to the sql server, check the sql server's firewall restrictions.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment