From af5e9c1bc13fe76d27621f226fd3cba65894ed0f Mon Sep 17 00:00:00 2001 From: Jacob McDonnell Date: Wed, 3 May 2023 15:08:15 -0400 Subject: Finished Tutorial on flask website --- requirements.txt | 0 static/articles/flaskwebsite/flaskwebsite.md | 147 ++++++++++++++++++++- ...ebsite.sync-conflict-20230503-132338-Q37XHXN.md | 1 - 3 files changed, 146 insertions(+), 2 deletions(-) mode change 100644 => 100755 requirements.txt mode change 100644 => 100755 static/articles/flaskwebsite/flaskwebsite.md delete mode 100755 static/articles/flaskwebsite/flaskwebsite.sync-conflict-20230503-132338-Q37XHXN.md diff --git a/requirements.txt b/requirements.txt old mode 100644 new mode 100755 diff --git a/static/articles/flaskwebsite/flaskwebsite.md b/static/articles/flaskwebsite/flaskwebsite.md old mode 100644 new mode 100755 index c08da37..2dcaac7 --- a/static/articles/flaskwebsite/flaskwebsite.md +++ b/static/articles/flaskwebsite/flaskwebsite.md @@ -2,5 +2,150 @@ This article assumes you have completed up to setting up Nginx based on the [Last Article](/articles/rpilinuxserver/), or that you already have a server setup. -## +## Configuring Nginx + +First, make your folder for the website, this is where your website will live: + +

+sudo mkdir -p /var/www/websiteName
+
+ +Next, we need to set the proper permissions to make sure everything works: + +

+sudo chown -R nginx /var/www/websiteName
+sudo chmod -R 755 /var/www/websiteName
+
+ +Now, we will create the config file for website: + +

+sudo nano /etc/nginx/conf.d/websiteName.conf
+
+ +and paste the following into the file: + +

+server {
+        listen 80;
+        server_name example.com www.example.com;
+
+        location / {
+                proxy_pass http://127.0.0.1:8000/;
+                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+                proxy_set_header X-Forwarded-Proto $scheme;
+                proxy_set_header X-Forwarded-Host $host;
+                proxy_set_header X-Forwarded-Prefix /;
+        }
+}
+
+ +Now, confirm that the nginx configuration is ok: + +

+sudo nginx -t
+
+ +Restart nginx: + +

+sudo systemctl restart nginx
+sudo systemctl status nginx
+
+ +Next, set SELinux to permissive mode: + +

+sudo setenforce permissive
+sudo getenforce
+
+ +Now, we will need to set SELinux to permissive mode permanently: + +

+sudo sed -i --follow-symlinks 's/SELINUX=enforcing/SELINUX=permissive/g' /etc/sysconfig/selinux
+
+ +## Running the Flask App + +### Install Gunicorn + +To run the your Flask website you need to install gunicorn. + +

+pip install gunicorn
+sudo cp ~/.local/bin/gunicorn /usr/bin/gunicorn
+
+ +### Configure Systemd + +You will need to create a systemd service for gunicorn. + +In `/etc/systemd/system/yourapp.service` + +

+[Unit]
+Description = yourapp
+After = network.target
+
+[Service]
+PermissionsStartOnly = true
+PIDFile = /run/yourapp/yourapp.pid
+User = gunicorn
+Group = gunicorn
+WorkingDirectory = /var/www/yourapp
+ExecStartPre = /bin/mkdir /run/yourapp
+ExecStartPre = /bin/chown -R gunicorn:gunicorn /run/yourapp
+ExecStart = /usr/bin/gunicorn main:app -b 0.0.0.0:8000 --pid /run/yourapp/yourapp.pid
+ExecReload = /bin/kill -s HUP $MAINPID
+ExecStop = /bin/kill -s TERM $MAINPID
+ExecStopPost = /bin/rm -rf /run/yourapp
+PrivateTmp = true
+
+[Install]
+WantedBy = multi-user.target
+
+ +Now you will need to run the following commands: + +

+sudo systemctl daemon-reload
+sudo systemctl enable yourapp
+sudo systemctl start yourapp
+
+ +At this point when you navigate to your website, it should load. + +## Installing and Running Certbot + +To install Certbot run: + +

+sudo dnf install certbot python3-certbot-nginx
+
+ +To get SSL certificates for your websites run: + +

+sudo certbot --nginx
+
+ +Answer the prompts that show up on screen as you wish. + +To configure auto renewal of the SSL certificate run: + +

+crontab -e
+
+ +and add the following line: + +

+0 12 * * * /usr/bin/certbot renew --quiet
+
+ +This will check everyday at noon to see if the certificate will expire in the +next month, if so it will renew the certificate. + +Now your website should be operational. diff --git a/static/articles/flaskwebsite/flaskwebsite.sync-conflict-20230503-132338-Q37XHXN.md b/static/articles/flaskwebsite/flaskwebsite.sync-conflict-20230503-132338-Q37XHXN.md deleted file mode 100755 index 792d600..0000000 --- a/static/articles/flaskwebsite/flaskwebsite.sync-conflict-20230503-132338-Q37XHXN.md +++ /dev/null @@ -1 +0,0 @@ -# -- cgit v1.2.3