Setup Local(macOS) Python Web Environment

Given the target path is "/root/home/python"

# Step 1: install homebrew

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

# Step 2: install python with homebrew

cd /roo/home/pyton

brew install python

# Step 3: set 'python' as the alias of 'python3'

vim ~/.zshrc

alias python='python3'

# :wq save and exit;

source ~/.zshrc

# Step 4: create virtual environment (env name: newenv / project name: newpro)

python -m venv newenv

source myenv/bin/activate

pip install django

django-admin startproject newpro

cd newpro

python manage.py runserver

# now you can visit http://127.0.0.1:8000 with explore and if you can see the "The install worked successfully! Congratulations! Django" it means running well.

# Step 5: install nginx and configure

brew install nginx

cd /usr/local/etc/nginx

vim nginx.conf

worker_processes  1;

error_log  /usr/local/var/log/nginx/error.log warn;
pid        /usr/local/var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /usr/local/var/log/nginx/access.log main;

    sendfile        on;
    tcp_nopush     on;
    tcp_nodelay    on;
    keepalive_timeout  65;

    include /usr/local/etc/nginx/servers/*.conf;
}

# :wq save and exit

cd /usr/local/etc/nginx/servers/

vim django.conf

server {
    listen 80;
    server_name localhost;

    location / {
        proxy_pass http://127.0.0.1:8000;  # 将请求转发到 Django 开发服务器
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

# :wq save and exit

sudo nginx -s reload

# # now you can visit http://127.0.0.1 with explore and if you can see the "The install worked successfully! Congratulations! Django" it means running well.

# the structure of the target directory should be as below:

/root/home/python
├── newenv
└── newpro
    ├── manage.py
    └── newpro/
        ├── __init__.py
        ├── asgi.py
        ├── settings.py
        ├── urls.py
        └── wsgi.py

## Django server is suitable for developing environment and purpose, if you want to run a productive environment, uWSGI will be a good choice, configration as below:

# Step 1:

pip install uwsgi

# Step 2:

cd /root/home/python/newpro/newpro/

vim uwsgi.ini

[uwsgi]
# 设置 Django 项目的根目录
chdir = /root/home/python/newpro/newpro

# 指定 WSGI 应用的模块
module = newpro.wsgi:application

# 运行 uWSGI 服务器的端口
http = 127.0.0.1:8000

# 进程和线程配置
master = true
processes = 4
threads = 2

# 日志配置
logto = /var/log/uwsgi/uwsgi.log

# 自动重新加载
touch-reload = /root/home/python/newpro/wsgi.py

# 启用虚拟环境
virtualenv = /root/home/python/newenv

# :wq save and exit

uwsgi --ini uwsgi.ini

# Step 3:

vim /usr/local/etc/nginx/servers/django.conf

server {
    listen 80;
    server_name localhost;

    location / {
        include uwsgi_params;
        uwsgi_pass 127.0.0.1:8000;
    }
}

# Step 4: check all the following fiels exist, as below:

cat /usr/local/etc/nginx/uwsgi_params

# the uwsgi server address
uwsgi_param  UWSGI_SCHEME $scheme;
uwsgi_param  SERVER_PORT $server_port;
uwsgi_param  SERVER_NAME $server_name;

# These parameters are used to pass client information
uwsgi_param  REMOTE_ADDR $remote_addr;
uwsgi_param  REMOTE_PORT $remote_port;
uwsgi_param  SERVER_ADDR $server_addr;
uwsgi_param  SERVER_PORT $server_port;

# Request related headers
uwsgi_param  HTTP_HOST $host;
uwsgi_param  HTTP_USER_AGENT $http_user_agent;
uwsgi_param  HTTP_X_REQUESTED_WITH $http_x_requested_with;
uwsgi_param  HTTP_REFERER $http_referer;
uwsgi_param  HTTP_X_FORWARDED_FOR $http_x_forwarded_for;
uwsgi_param  HTTP_X_FORWARDED_PROTO $http_x_forwarded_proto;
uwsgi_param  HTTP_X_FORWARDED_PORT $http_x_forwarded_port;

# the maximum number of bytes that can be uploaded
uwsgi_param  CONTENT_LENGTH $content_length;
uwsgi_param  CONTENT_TYPE $content_type;

# Request method and protocol
uwsgi_param  REQUEST_METHOD $request_method;
uwsgi_param  REQUEST_URI $request_uri;
uwsgi_param  QUERY_STRING $query_string;
uwsgi_param  REQUEST_SCHEME $scheme;
uwsgi_param  HTTPS $https;

# HTTP version
uwsgi_param  HTTP_VERSION $server_protocol;

 

Scroll to Top