综合实现“centos+nginx+uwsgi+Django实现IP+port访问服务器”需要完成以下几个步骤:
- 安装必要的软件和库,其中包括centos、nginx、uwsgi、Django等,具体操作如下:
# 安装centos
sudo yum update
sudo yum install epel-release
sudo yum install centos-release-scl
sudo yum install python-pip python-dev nginx uwsgi uwsgi-plugin-python3
# 安装Django
sudo pip install virtualenv
mkdir /app && cd /app
virtualenv env
source env/bin/activate
pip install Django
# 安装uwsgi
sudo pip install uwsgi
# 安装nginx
sudo yum install -y nginx
- 配置nginx,将其配置成反向代理服务器,将请求转发到uwsgi,具体操作如下:
# 在/etc/nginx/conf.d/目录下创建.conf文件
sudo vim /etc/nginx/conf.d/myproject.conf
# 文件内容如下:
server {
listen 8000;
client_max_body_size 75M;
location / {
proxy_pass http://127.0.0.1:8001; # 8001为uwsgi监听的端口号
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
# 配置完成后重启nginx
sudo systemctl restart nginx
- 配置uwsgi,通过uwsgi将Django应用与nginx关联起来,具体操作如下:
# 创建uwsgi配置文件uwsgi.ini
[uwsgi]
# Django相关配置
module = myproject.wsgi:application
env = DJANGO_SETTINGS_MODULE=myproject.settings
master = true
processes = 4
harakiri = 60
max-requests = 5000
vacuum = true
# socket相关配置
socket = 127.0.0.1:8001 # 8001为uwsgi监听的端口号
chmod-socket = 664
# 日志相关配置
logto = /var/log/uwsgi/%n.log
# 启动uwsgi
uwsgi --ini /path/to/uwsgi.ini
- 测试Django应用是否正常工作,具体操作如下:
# 创建Django应用
django-admin startproject myproject
cd myproject
# 启动Django开发服务器
python manage.py runserver 0.0.0.0:8000
# 访问服务器IP和端口,比如http://192.168.1.10:8000/
# 如果可以成功访问,说明Django应用已经正常工作
-
正式部署Django项目,将其部署到uwsgi和nginx上,具体操作步骤如下:
-
上传Django项目到服务器
- 使用uwsgi启动Django项目
- 使用nginx设置反向代理
示例1:将Django项目部署到uwsgi和nginx上
# 1. 上传Django项目到服务器
# 2. 使用uwsgi启动Django项目
# 在项目目录下创建文件uwsgi.ini,内容如下:
[uwsgi]
# Django相关配置
module = myproject.wsgi:application
env = DJANGO_SETTINGS_MODULE=myproject.settings
master = true
processes = 4
harakiri = 60
max-requests = 5000
vacuum = true
# socket相关配置
socket = 127.0.0.1:8001 # 8001为uwsgi监听的端口号
chmod-socket = 664
# 日志相关配置
logto = /var/log/uwsgi/%n.log
# 启动uwsgi
uwsgi --ini /path/to/uwsgi.ini
# 3. 使用nginx设置反向代理
# 在/etc/nginx/conf.d/目录下创建.conf文件,内容如下:
server {
listen 80;
server_name example.com;
root /path/to/myproject;
error_log /var/log/nginx/myproject.error.log;
access_log /var/log/nginx/myproject.access.log;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8001; # 8001为uwsgi监听的端口号
}
}
# 配置完成后重启nginx
sudo systemctl restart nginx
示例2:在Django项目中启用HTTPS
# 1. 购买证书
# 2. 在nginx中配置HTTPS
# 在/etc/nginx/conf.d/目录下创建.conf文件,内容如下:
server {
listen 80;
server_name example.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name example.com;
ssl_certificate /path/to/ssl.crt;
ssl_certificate_key /path/to/ssl.key;
root /path/to/myproject;
error_log /var/log/nginx/myproject.error.log;
access_log /var/log/nginx/myproject.access.log;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8001; # 8001为uwsgi监听的端口号
}
}
# 配置完成后重启nginx
sudo systemctl restart nginx
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:centos+nginx+uwsgi+Django实现IP+port访问服务器 - Python技术站