Skip to content

Commit 20460bf

Browse files
committed
Starter files for deployment chapter.
1 parent 125b6ac commit 20460bf

File tree

3 files changed

+133
-0
lines changed

3 files changed

+133
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
server {
2+
listen 80;
3+
server_name guitary.talkpython.com;
4+
server_tokens off;
5+
charset utf-8;
6+
client_max_body_size 150M;
7+
8+
___location /static {
9+
gzip on;
10+
gzip_buffers 8 256k;
11+
uwsgi_buffers 8 256k;
12+
13+
alias /apps/app_repo/apps/py/ch07_web/guitary/static;
14+
expires 365d;
15+
}
16+
___location / {
17+
try_files $uri @yourapplication;
18+
}
19+
___location @yourapplication {
20+
include uwsgi_params;
21+
22+
gzip on;
23+
gzip_buffers 8 256k;
24+
uwsgi_buffers 8 256k;
25+
uwsgi_read_timeout 300;
26+
27+
proxy_pass http://127.0.0.1:5000;
28+
proxy_set_header Host $host;
29+
proxy_set_header X-Real-IP $remote_addr;
30+
proxy_set_header X-Forwarded-Protocol $scheme;
31+
}
32+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[Unit]
2+
Description=uWSGI PyPI server instance
3+
After=syslog.target
4+
5+
[Service]
6+
ExecStart=/apps/venv/bin/uwsgi -H /apps/venv --master --processes 4 --threads 2 --http :5000 --manage-script-name --python-path /apps/app_repo/apps/py/ch07_web --mount /=wsgi:app --logto /apps/logs/guitary/uwsgi.log
7+
RuntimeDirectory=/apps/app_repo/apps/py/ch07_web
8+
Restart=always
9+
KillSignal=SIGQUIT
10+
Type=notify
11+
StandardError=syslog
12+
NotifyAccess=all
13+
14+
[Install]
15+
WantedBy=multi-user.target
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#!/usr/bin/env bash
2+
3+
# Consider running these two commands separately
4+
# Do a reboot before continuing.
5+
apt update
6+
apt upgrade -y
7+
8+
apt install zsh
9+
sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
10+
11+
# Install some OS dependencies:
12+
sudo apt-get install -y -q build-essential git unzip zip nload tree
13+
sudo apt-get install -y -q python3-pip python3-dev python3-venv
14+
sudo apt-get install -y -q nginx
15+
# for gzip support in uwsgi
16+
sudo apt-get install --no-install-recommends -y -q libpcre3-dev libz-dev
17+
18+
# Stop the hackers
19+
sudo apt install fail2ban -y
20+
21+
ufw allow 22
22+
ufw allow 80
23+
ufw allow 443
24+
ufw enable
25+
26+
# Basic git setup
27+
git config --global credential.helper cache
28+
git config --global credential.helper 'cache --timeout=720000'
29+
30+
# Be sure to put your info here:
31+
git config --global user.email "[email protected]"
32+
git config --global user.name "Your name"
33+
34+
# Web app file structure
35+
mkdir /apps
36+
chmod 777 /apps
37+
mkdir /apps/logs
38+
mkdir /apps/logs/guitary
39+
mkdir /apps/logs/guitary/app_log
40+
cd /apps
41+
42+
# Create a virtual env for the app.
43+
cd /apps
44+
python3 -m venv venv
45+
source /apps/venv/bin/activate
46+
pip install --upgrade pip setuptools
47+
pip install --upgrade httpie glances
48+
pip install --upgrade uwsgi
49+
50+
51+
# clone the repo:
52+
cd /apps
53+
git clone https://github.com/mikeckennedy/python-for-dotnet-developers-materials app_repo
54+
55+
# Setup the web app:
56+
cd /apps/app_repo/apps/py/ch07_web/
57+
pip install -r requirements.txt
58+
59+
# Copy and enable the daemon
60+
cp /apps/app_repo/apps/py/Ch10_deploy/config/guitary.service /etc/systemd/system/guitary.service
61+
62+
63+
systemctl start guitary
64+
systemctl status guitary
65+
systemctl enable guitary
66+
67+
# systemctl daemon-reload
68+
# If you make changes to the service file
69+
70+
# Setup the public facing server (NGINX)
71+
apt install nginx
72+
73+
# CAREFUL HERE. If you are using default, maybe skip this
74+
rm /etc/nginx/sites-enabled/default
75+
76+
cp /apps/app_repo/apps/py/ch12_deployment/config/guitary.nginx /etc/nginx/sites-enabled/guitary.nginx
77+
update-rc.d nginx enable
78+
service nginx restart
79+
80+
81+
# Optionally add SSL support via Let's Encrypt:
82+
# https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-18-04
83+
84+
add-apt-repository ppa:certbot/certbot
85+
apt install python-certbot-nginx
86+
certbot --nginx -d guitary.talkpython.com

0 commit comments

Comments
 (0)