The first step is to install all of the necessary packages from the default Ubuntu repositories. This includes pip, the Python package manager, which will manage your Python components. You’ll also get the Python development files necessary to build some of the Gunicorn components.
First, update the local package:
sudo apt update
Then install the packages that will allow you to build your Python environment. These include python3-pip, along with a few more packages and development tools necessary for a robust programming environment:
sudo apt install python3-pip python3-dev build-essential libssl-dev libffi-dev python3-setuptools
With these packages in place, move on to creating a virtual environment for your project.
Next, set up a virtual environment to isolate your Flask application from the other Python files on the system.
Start by installing the python3-venv package, which will install the venv module:
sudo apt install python3-venv
Next, make a parent directory for your Flask project:
mkdir ~/myproject
Then change into the directory after you create it:
cd ~/myproject
Create a virtual environment to store your Flask project’s Python requirements by entering the following:
python3 -m venv myprojectenv
This will install a local copy of Python and pip into a directory called myprojectenv within your project directory.
Before installing applications within the virtual environment, you need to activate it by running the following:
source myprojectenv/bin/activate
Your prompt will change to indicate that you are now operating within the virtual environment. It will read like the following:
(myprojectenv)\ssammy@host:~/myproject$
Now that you are in your virtual environment, you can install Flask and Gunicorn and get started on designing your application.
First, install wheel with the local instance of pip to ensure that your packages will install even if they are missing wheel archives:
pip install wheel
Next, install Flask and Gunicorn:
pip install gunicorn flask
Next, clone your flask project from github
git clone myproject
change directory to your project and install all requirements
cd mygithubproject
then
pip install -r requirements.txt
still in your project directory running
gunicorn --bind app:app
Visit your server’s IP address with :8000 appended to the end in your web browser again:
http://your_server_ip:8000
When you have confirmed that it’s functioning properly, press CTRL + C in your terminal window.
Since now you’re done with your virtual environment, deactivate it:
deactivate
Any Python commands will now use the system’s Python environment again.
Next, create the systemd service unit file. Creating a systemd unit file will allow Ubuntu’s init system to automatically start Gunicorn and serve the Flask application whenever the server boots.
Create a unit file ending in .service within the /etc/systemd/system directory to begin:
sudo nano /etc/systemd/system/myproject.service
modify the following to add your project name, user and working directory:
[Unit]
Description=Gunicorn instance to serve myproject
After=network.target
[Service]
User=sammy
Group=www-data
WorkingDirectory=/home/sammy/myproject
Environment="PATH=/home/sammy/myproject/myprojectenv/bin"
ExecStart=/home/sammy/myproject/myprojectenv/bin/gunicorn --workers 3 --bind unix:myproject.sock -m 007 wsgi:app
[Install]
WantedBy=multi-user.target
With that, your systemd service file is complete. Save and close it now.
Now start the Gunicorn service you created:
sudo systemctl start myproject
Then enable it so that it starts at boot:
sudo systemctl enable myproject
Check the status:
sudo systemctl status myproject
You should receive output like the following:
myproject.service - Gunicorn instance to serve myproject
Loaded: loaded (/etc/systemd/system/myproject.service; enabled; vendor preset
Active: active (running) since Fri 2021-11-19 23:08:44 UTC; 6s ago
Main PID: 8770 (gunicorn)
Tasks: 4 (limit: 1151)
CGroup: /system.slice/myproject.service
├─9291 /home/sammy/myproject/myprojectenv/bin/python3.6 /home/sammy/myproject/myprojectenv/bin/gunicorn --workers 3 --bind unix:myproject.sock -m 007 app:app
├─9309 /home/sammy/myproject/myprojectenv/bin/python3.6 /home/sammy/myproject/myprojectenv/bin/gunicorn --workers 3 --bind unix:myproject.sock -m 007 app:app
├─9310 /home/sammy/myproject/myprojectenv/bin/python3.6 /home/sammy/myproject/myprojectenv/bin/gunicorn --workers 3 --bind unix:myproject.sock -m 007 app:app
└─9311 /home/sammy/myproject/myprojectenv/bin/python3.6 /home/sammy/myproject/myprojectenv/bin/gunicorn --workers 3 --bind unix:myproject.sock -m 007 app:app
sudo apt install nginx
Your Gunicorn application server should now be up and running, waiting for requests on the socket file in the project directory. Next, configure Nginx to pass web requests to that socket by making some small additions to its configuration file.
Begin by creating a new server block configuration file in Nginx’s sites-available directory. We’ll call this myproject to stay consistent with the rest of the guide:
sudo nano /etc/nginx/sites-available/myproject
add the following:
server {
listen 80;
server_name your_domain www.your_domain;
location / {
include proxy_params;
proxy_pass http://127.0.0.1:8000;
}
}
Save and close the file when you’re finished.
To enable the Nginx server block configuration you’ve created, link the file to the sites-enabled directory. You can do this by running the ln command and the -s flag to create a symbolic or soft link, as opposed to a hard link:
sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled
With the link in that directory, you can test for syntax errors:
sudo nginx -t
If this returns without indicating any issues, restart the Nginx process to read the new configuration:
sudo systemctl restart nginx
You should now be able to navigate to your server’s domain name in your web browser
http://your_domain
If you encounter any errors, try checking the following:
sudo less /var/log/nginx/error.log: checks the Nginx error logs. sudo less /var/log/nginx/access.log: checks the Nginx access logs. sudo journalctl -u nginx: checks the Nginx process logs. sudo journalctl -u myproject: checks your Flask app’s Gunicorn logs.
checkout this readme file on how to setup postgress database on ubuntu vm
Checkout this readme filr on how to setup ssl certificate on your site