nginx+uwsgi+django 部署

环境准备:

ubuntun 20.04

python 3.8.10

基础配置

如果是云环境做一下修改,否则跳过:

sudo vim/etc/passwd 修改用户等级

sudo vim/etc/sudoers 修改权限

sudo vim /etc/ssh/sshd_config 修改ssh服务器

修改 PermitRootLogin= yes --允许以root用户登录

---------------------------------------------------------------------------------------------------------------------------------

使用xftp复制项目到/var/下  

安装MySQL 数据库

sudo apt update

sudo apt install mysql-server

一般安装完后,MYSQL服务都会自动启动哦。查看MYSQL是否正常运行:

sudo systemctl status mysql

快速配置mysql,除了配置root账号的密码以外其他全部都选择yes

sudo mysql_secure_installation

安装mysqlclient

sudo apt-get install python-dev --python2使用这个安装

sudo apt-get install python3-dev

sudo apt-get install libmysqlclient-dev

pip install mysqlclient

安装django

pip install django


安装DRF

pip install djangorestframework


安装Django跨域插件

pip install django-cors-headers

安装DjangoRedis插件

pip install Django-redis

pip install drf-extensions

安装Redis

sudo apt-get install -y redis-server

修改配置文件:

cd 到 /etc/redis/目录下

vim redis_config

bind 127.0.0.1 ::1” 注释这行

“daemonize yes”,将yes改成no

如果要添加登录密码,找到“requirepass”,在后边写自己的密码 在500行

prodetced-mode yes 将yes修改成no 在88行

service redis restart 重启redis

安装uwsgi以及配置uwsgi

pip install uwsgi

在Django的项目根目录(有manage.py的目录)下新建uwsgi.ini文件,配置内容如下(拷贝后将‘=’后面的内容改一下):

[uwsgi]
#服务端口
http = :8000

#指定与Nginx通信的方式,不影响uwsgi本身运行。如果配置了需要到nginx中进行相关配置-才能通过nginx访问Django
# socket = 127.0.0.1:8000

# 启动一个master进程,来管理其余的子进程
master = True
processes = 4
threads = 2

#python虚拟环境目录绝对路径。如果有的话,home是虚拟环境根目录,PYTHNONHOME是虚拟环境下的bin目录(放k置了Python执行文件)
#home = /env
#PYTHONHOME = /env/bin

#django项目目录,与manager.py同级
;chdir = /var/www/项目名
chdir = /var/DRFS/

#主应用中的wsgi,下面这种配法是在Django根目录下运行uwsgi有效,主APP名为有settings.py的那个目录名。如果是其他目录运行,下面建议写成绝对路径。
;wsgi-file = 主APP名/wsgi.py
wsgi-file = DRFS/wsgi.py
#服务停止时自动移除unix Socket和pid文件
vacuum = true

#设置每个工作进程处理请求的上限,达到上限时,将回收(重启)进程,可以预防内存泄漏
max-requests = 32768

#设置后台运行保存日志。只要配置了daemonize就会让uwsgi后台运行,同时将日志输出到指定目录
daemonize = /var/www/log

#保存主进程的pid,用来控制uwsgi服务
pidfile = uwsgi.pid
#uwsgi --stop/reload xxx.pid 停止/重启uwsgi

#静态文件映射
#static-map = /static=Django下static目录的绝对路径

sudo ln -s /usr/local/python3.8.10/bin/uwsgi /usr/bin/uwsgi

3.1 启动
切换到 uwsgi.ini 文件所在的路径
执行 uwsgi --ini uwsgi.ini 
测试,浏览器打开相应的URL进行测试
3.2 停止 
切换到 uwsgi.ini 文件所在的路径
执行 uwsgi --stop uwsgi.pid 
3.3重启
执行:uwsgi --reload uwsgi.pid

测试Django是否成功

python manage.py runserver 0.0.0.0:8000

测试wsgi

uwsgi --http-socket :8000 --plugins python3 --module DRFS.wsgi

安装ngix

sudo apt update

sudo apt install nginx

vim /etc/nginx/sites-enabled/default --配置ngix

##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# https://www.nginx.com/resources/wiki/start/
# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
# https://wiki.debian.org/Nginx/DirectoryStructure
#
# In most cases, administrators will remove this file from sites-enabled/ and
# leave it as reference inside of sites-available where it will continue to be
# updated by the nginx packaging team.
#
# This file will automatically load configuration files provided by other
# applications, such as Drupal or Wordpress. These applications will be made
# available underneath a path with that package name, such as /drupal8.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# Default server configuration
#
server {
	listen 80 default_server;
	listen [::]:80 default_server;

	# SSL configuration
	#
	# listen 443 ssl default_server;
	# listen [::]:443 ssl default_server;
	#
	# Note: You should disable gzip for SSL traffic.
	# See: https://bugs.debian.org/773332
	#
	# Read up on ssl_ciphers to ensure a secure configuration.
	# See: https://bugs.debian.org/765782
	#
	# Self signed certs generated by the ssl-cert package
	# Don't use them in a production server!
	#
	# include snippets/snakeoil.conf;

	root /var/www/html;

	# Add index.php to the list if you are using PHP
	index index.html index.htm index.nginx-debian.html;

	server_name _;

	location / {
		# First attempt to serve request as file, then
		# as directory, then fall back to displaying a 404.
		#try_files $uri $uri/ =404;
		uwsgi_pass 127.0.0.1:8000;
                include /etc/nginx/uwsgi_params;

	}

	# pass PHP scripts to FastCGI server
	#
	#location ~ \.php$ {
	#	include snippets/fastcgi-php.conf;
	#
	#	# With php-fpm (or other unix sockets):
	#	fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
	#	# With php-cgi (or other tcp sockets):
	#	fastcgi_pass 127.0.0.1:9000;
	#}

	# deny access to .htaccess files, if Apache's document root
	# concurs with nginx's one
	#
	#location ~ /\.ht {
	#	deny all;
	#}
}


# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
#	listen 80;
#	listen [::]:80;
#
#	server_name example.com;
#
#	root /var/www/example.com;
#	index index.html;
#
#	location / {
#		try_files $uri $uri/ =404;
#	}
#}

service nginx restart/start/stop/status  --nginx 控制

如果报错500一般是程序问题

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值