Setup Python Web Environment on LNMP
Typer | Posted on | |
# Given the target directories tree as below, the website is 'abc.com', the virtual environment with name 'env', the project's name is 'pro'
/etc/python/abc.com/
├── uwsgi.ini
├── env/ # 虚拟环境文件夹
├── pro/ # 项目文件夹
│ ├── db.sqlite3
│ ├── manage.py
│ ├── media/
│ ├── static/
│ ├── pro/ # 子文件夹,包含Django项目的核心配置
│ │ ├── asgi.py
│ │ ├── __init__.py
│ │ ├── __pycache__/
│ │ ├── settings.py
│ │ ├── templates/
│ │ ├── urls.py
│ │ ├── views.py
│ │ ├── wsgi.py
# Step 1:
cd /etc/python/
mkdir -p abc.com
cd abc.com
sudo apt update
sudo apt install python3 python3-pip python3-venv python3-dev libmysqlclient-dev build-essential
# Step 2:
cd /etc/python/abc.com/
python3 -m venv env
source env/bin/activate
pip install django
# Step 3:
sudo vim uwsgi.ini
# paste these content into it then save and exit (:wq)
[uwsgi]
chdir = /etc/python/abc.com/pro
module = pro.wsgi:application
env = DJANGO_SETTINGS_MODULE=pro.settings
home = /etc/python/abc.com/env
socket = /run/uwsgi/pro.sock
chmod-socket = 666
vacuum = true
plugin = python3
# Step 4:
cd /etc/systemd/system/
sudo vim uwsgi.service
# paste these content into it then save and exit (:wq)
[Unit]
Description=uWSGI Service
After=network.target
[Service]
User=www-data
Group=www-data
WorkingDirectory=/etc/python/abc.com/pro
ExecStart=/etc/python/abc.com/env/bin/uwsgi --ini /etc/python/abc.com/uwsgi.ini
[Install]
WantedBy=multi-user.target
sudo systemctl enable uwsgi
sudo systemctl start uwsgi
# Step 5:
cd /etc/python/abc.com/pro/
python manage.py collectstatic
sudo vim settings.py
# modify these content, then save and exit
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
MEDIA_ROOT = os.path.join(BASE_DIR, "media/")
ALLOWED_HOSTS = ['abc.com', 'www.abc.com']
DEBUG = False
# Step 6:
cd /etc/nginx/sites-available/
sudo vim abc.com.conf
# paste these content into it then save and exit (:wq)
server {
listen 80;
server_name abc.com www.abc.com;
location / {
include uwsgi_params;
uwsgi_pass unix:/run/uwsgi/pro.sock;
}
location /static/ {
alias /etc/python/abc.com/pro/static/;
}
location /media/ {
alias /etc/python/abc.com/pro/media/;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/abc.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/abc.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
}
server {
if ($host = www.abc.com) {
return 301 https://$host$request_uri;
}
if ($host = abc.com) {
return 301 https://$host$request_uri;
}
listen 80;
server_name abc.com www.abc.com;
return 404;
}
# Step 7:
sudo ln -s /etc/nginx/sites-available/abc.com /etc/nginx/sites-enabled/
certbot -d abc.com --nginx -v
sudo nginx -t
sudo systemctl reload nginx
sudo chown -R www-data:www-data /etc/python/abc.com/
# Step 8:
sudo mkdir /run/uwsgi
sudo chown www-data:www-data /run/uwsgi
sudo systemctl restart uwsgi
# Step 9:
cd /etc/tmpfiles.d/uwsgi.conf
vim uwsgi.conf
# # paste these content into it then save and exit (:wq)
d /run/uwsgi 0755 www-data www-data -
# Step 10: Check the status of nginx and uwsgi, if runs well, the abc.com or www.abc.com can display correctly.
cd /etc/python/abc.com
python manage.py collectstatic
systemctl restart nginx && systemctl restart uwsgi
systemctl status nginx && systemctl status uwsgi
# Step 11: modify the default admin entrance for abc.com:
cd /etc/python/abc.com/pro/pro
sudo vim urls.py
# change 'admin' to something you want, such as 'ksdfelkj' as bellow:
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.home, name='home'), # 配置根路径的 URL
]
# save and exit, then restart uwsgi as below:
sudo systemctl restart uwsgi && sudo systemctl status uwsgi