This is a gist containing several files needed to get Masonite automatically deploying to your servers via GitHub pushes (or releases)
NOTE: This script will have a downtime of a few seconds between deployments. If your application requires no downtime you can see this GIST here for a bit more complex GIST for getting to zero downtime deployments through the use of uWSGI, unix sockets and managed config files
- NGINX installed (may or may not be fully configured)
- Python 3 installed and everything needed to run a Masonite application (see Masonite documentation for requirements)
- Linux packages installed required to install Python packages (see Masonite documentation again)
- Git installed
- SSH credentials to connect to the server you are deploying to
- Need to have the
gunicorn
requirement in yourrequirements.txt
file
First things first. The first thing you need to do is have NGINX installed. This Gist assumes you have that installed already.
At high level there are 3 things we have to do:
- We need to make sure our NGINX config is setup.
- We need to make sure we have a config file for our actual application
- We need to get the workflow in our application so GitHub will know to run it on certain actions (pushing or releasing)
First we need to locate where our NGINX config file lives. Simply run this:
$ nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
When you get the output, you are looking for the configuration file. Both directories should be the same.
We can view and edit this file by doing:
$ nano /etc/nginx/nginx.conf
If you already have an NGINX config file then you may not want to override the existing file. The real important part is are these 2 lines in this block:
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
This will tell NGINX to additionally load config files from these directories. We will use the bottom directory to put the app-config.conf
file in this gist. If you already have these 2 lines (or lines similiar to these) then you are good. You can exit out by hitting CTRL+X
. If you get some kind of prompt that says "Save modified buffer? (Y/n)" just hit "Y" and hit enter. This just tells nano to save the file. If you don't want to save it then enter n and hit enter.
Once done we need to then go to one of those directories. It really doesn't matter which one but let's pick the bottom one
$ cd /etc/nginx/sites-enabled
We can then make our config file. You can name this whatever you want. Maybe its the name of your application:
$ nano exampleapp.conf
This should open an empty editor. You can then paste in app-config.conf
you see in this Gist.
You may need to make some modifications to it.
Going from top to bottom, we should be listening on port 80.
The next line says server_name
. If you put your IP address then it will work when you go to http://17.62.11.87
. What you most likely want is a domain name so you can change it to a domain name instead.
The next is location /
. We can leave that as is
The next line is include uwsgi_params;
. leave that as well.
The next line says proxy_pass http://127.0.0.1:8000;
. everything here can stay but you may want to modify the port. You will be running your application on a specific port with gunicorn (or a related wsgi server) and NGINX needs to know which port you are going to do that on.
All said and done you might have something like looks like this:
## /etc/nginx/sites-enabled/app-config.conf
## Name this file whatever you want. Just make sure it goes in the /etc/nginx/sites-enabled/ directory
server {
listen 80;
- server_name 17.62.11.87;
+ server_name subdomain.example.com;
location / {
include uwsgi_params;
- proxy_pass http://127.0.0.1:8000;
+ proxy_pass http://127.0.0.1:8001;
}
}
Once done you can back out of nano again.
At this point you may need to reload nginx. To do that just run:
nginx -s reload
The last stop now that the server is up and running is you will need to get the workflow into your project so GitHub will pick up on it and run it on various actions like pushing or releasing.
You can do this by starting in your project (or GitHub repo) and create a .github/workflows/deploy.yml
file. Then copy and paste the workflow to that file (the masonite-deploy.yml
file in this Gist). Commit the change and push it up to the origin repository on GitHub.
Next we need to configure some github secrets. You can go to your repo and look in the top right of the page. You should see "Setting". Then go to "Secrets" on the left vertical navigation bar near the bottom.
You'll need to add these secrets:
HOST: the server IP address you want to deploy to
USERNAME: the username of the ssh user
PASSWORD: the password of the ssh user
PORT: The port for ssh (usually 22)
ENV: A list of environment variables for your project. This should just be a list of rows with key value pairs like this:
KEY=123
APP_DEBUG=True
ENV3=value
Once done the GitHub action will run when you push (or release depending on what you set at the top, see the comment there). Feel free to modify the action if you want. There are comments next to each line to explain what they are doing.
sudo systemctl start project
sudo systemctl enable project