We are going to build a simple and smooth process using the following steps:
if you don’t have uwsgi installed, try this on ubuntu:
sudo pip3 install uwsgi
locate your flask project folder, if you don’t know how to create a a flask app, check this link:
flask app creation
https://datasciencebyexample.com/2021/09/29/2021-09-29-1/create a uswgi init file inside your flask project, for example, you can call it myproject.ini
[uwsgi]
module = main:app
master = true
processes = 2
socket = myproject.sock
chmod-socket = 660
vacuum = true
die-on-term = trueLet’s then create the systemd service unit file. Creating a systemd unit file will allow Ubuntu’s init system to automatically start uWSGI and serve the Flask application whenever the server boots.
Create a unit file ending in .service within the /etc/systemd/system directory to begin, for example
sudo emacs /etc/systemd/system/myproject.service |
inside the file:
[Unit] |
Then type these in the command line:
sudo systemctl start myproject |
you can check the service status
sudo systemctl status myproject |
if any changes to the project, just do this to reflect new web app:
sudo service myproject restart |
- install the nginx if you don’t have oneAt the end of the installation process, Ubuntu (18.04) will start Nginx. The web server should already be up and running.
sudo apt update
sudo apt install nginx
We can check with the systemd init system to make sure the service is running by typing:
systemctl status nginx |
Begin by creating a new server block configuration file in Nginx’s sites-available directory. Let’s call this myproject to keep in line with the rest of the guide:
sudo emacs /etc/nginx/sites-available/myproject
inside the file:
server {
listen 80;
server_name englishlearningbyexample.com www.englishlearningbyexample.com;
location / {
include uwsgi_params;
uwsgi_pass unix:<your flask app dir>/myproject.sock;
}
}above I was using
www.englishlearningbyexample.com
as the example for server_name, but you should replace with yours.final steps
To enable the Nginx server block configuration you’ve just created, link the file to the sites-enabled directory:
sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled |
then do
sudo systemctl restart nginx |
now you should see the website running
If you encounter any errors, trying checking the following:
sudo less /var/log/nginx/error.log: checks the Nginx error logs. |