django+vue项目部署到服务器(uwsgi+nginx)最新
1.安装python
[root@dsc1 nginx]# python -V
Python 3.6.5
2.安装数据库
yum install mysql mysql-devel
yum install sqlite-devel
3.安装django
[root@dsc1 mydjango]# pip3 install django
pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
Collecting django
Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.",)': /simple/django/
Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.",)': /simple/django/
Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.",)': /simple/django/
Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.",)': /simple/django/
Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.",)': /simple/django/
Could not fetch URL https://pypi.python.org/simple/django/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.python.org', port=443): Max retries exceeded with url: /simple/django/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.",)) - skipping
Could not find a version that satisfies the requirement django (from versions: )
No matching distribution found for django
解决办法:
yum install openssl-devel
重新编译安装python3
3.项目打包
这里的项目名称为:djangohxl,我们需要打包该目录
下面需要将该包上传到服务器
4.上传到指定目录解压
[root@dsc1 soft]#mkdir /deploy
[root@dsc1 soft]# mv djangohxl.rar /deploy/
[root@dsc1 deploy]# cd /deploy/
[root@dsc1 deploy]# rar x djangohxl.rar
windows下的rar文件上传到linux后需要安装rar才能解压
解压后的目录如下:
[root@dsc1 deploy]# ls -1
djangohxl
[root@dsc1 deploy]# cd djangohxl/
[root@dsc1 djangohxl]# ls
db.sqlite3 manage.py myweb templates venv
有依赖的需要安装依赖
[root@dsc1 mydjango]#cd /deploy/djangohxl
[root@dsc1 mydjango]#pip3 install -r requirements.txt
5.生成静态文件
修改配置文件(settings.py):
加入如下项目:
STATIC_ROOT = os.path.join(BASE_DIR,'static')
执行
[root@dsc1 djangohxl]# python manage.py collectstatic
128 static files copied to '/deploy/djangohxl/static'.
这个时候静态文件会拷贝到如下目录
[root@dsc1 uwsgi]# ls -al /deploy/djangohxl/static
total 0
drwxr-xr-x 3 root root 19 Jun 4 16:37 .
drwxr-xr-x 7 root root 110 Jun 4 16:37 ..
drwxr-xr-x 6 root root 51 Jun 4 16:37 admin
5.先测试一下不使用Nginx和uWSGI,django能不能正常运行
[root@dsc1 mydjango]# cd /deploy/djangohxl
[root@dsc1 mydjango]# python3 manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
May 31, 2024 - 01:14:10
Django version 3.2.25, using settings 'mydjango.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
指定本机ip访问
python3 manage.py runserver 192.168.1.102:8000
浏览器输入:
http://192.168.1.102:8000/
浏览器输入:
http://192.168.1.102:8000/admin
###################################安装uwsgi#######################################
1.安装
[root@dsc1 mydjango]#pip3 install uwsgi
[root@dsc1 mydjango]# uwsgi --version
2.0.25.1
2.创建uwsig使用的目录,该目录可自定义
[root@dsc1 deploy]# mkdir -p /deploy/uwsgi
3.编辑配置文件
vi /deploy/uwsgi/uwsgi.ini
[root@dsc1 uwsgi]# more uwsgi.ini
[uwsgi]
# Django-related settings
socket = 127.0.0.1:8000
# the base directory (full path)
chdir = /deploy/djangohxl
# Django s wsgi file
module = myweb.wsgi
wsgi-file=myweb/wsgi.py
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 4
# ... with appropriate permissions - may be needed
# chmod-socket = 664
# clear environment on exit
vacuum = true
pidfile=uwsgi.pid
daemonize=uwsgi.log
chdir = /deploy/djangohxl ##这里上项目的绝对路径
wsgi-file=myweb/wsgi.py ##root app的目录
8.启动测试
## 在当前目录启动
[root@dsc1 mydjango]# cd /deploy/uwsgi
[root@dsc1 mydjango]# uwsgi -i uwsgi.ini
[uWSGI] getting INI configuration from mydjango.ini
## 启动之后查看进程
[root@dsc1 mydjango]# ps -ef | grep uwsgi
root 18835 1 0 11:06 ? 00:00:00 uwsgi -i mydjango.ini
root 18836 18835 1 11:06 ? 00:00:00 uwsgi -i mydjango.ini
root 18837 18835 2 11:06 ? 00:00:00 uwsgi -i mydjango.ini
root 18838 18835 2 11:06 ? 00:00:00 uwsgi -i mydjango.ini
root 18839 18835 2 11:06 ? 00:00:00 uwsgi -i mydjango.ini
root 18849 16852 0 11:06 pts/2 00:00:00 grep --color=auto uwsgi
查看日志:
[root@dsc1 mydjango]# tail -200f uwsgi.log
*** Operational MODE: preforking ***
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
spawned uWSGI master process (pid: 18835)
spawned uWSGI worker 1 (pid: 18836, cores: 1)
spawned uWSGI worker 2 (pid: 18837, cores: 1)
spawned uWSGI worker 3 (pid: 18838, cores: 1)
spawned uWSGI worker 4 (pid: 18839, cores: 1)
Python auto-reloader enabled
WSGI app 0 (mountpoint='') ready in 2 seconds on interpreter 0x28f3090 pid: 18839 (default app)
WSGI app 0 (mountpoint='') ready in 2 seconds on interpreter 0x28f3090 pid: 18836 (default app)
WSGI app 0 (mountpoint='') ready in 2 seconds on interpreter 0x28f3090 pid: 18837 (default app)
WSGI app 0 (mountpoint='') ready in 2 seconds on interpreter 0x28f3090 pid: 18838 (default app)
服务正常启动
停掉uwsig命令:
[root@dsc1 uwsgi]# cd /deploy/uwsgi
[root@dsc1 uwsgi]#uwsgi --stop uwsgi.pid
##########################################部署nginx##################################
1.安装nginx
[root@dsc1 ~]#yum install -y nginx
2.修改配置文件
[root@dsc1 nginx]# more nginx.conf
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
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 /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 4096;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 8099;
server_name 192.168.1.102;
location /static {
alias /deploy/djangohxl/static; ##静态文件
}
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8000;
uwsgi_read_timeout 2;
}
}
##vue部分 vue项目build后生成的dist目录放到如下目录
server {
listen 8888;
server_name 192.168.1.102;
location / {
alias /deploy/djangohxl/static/dist/;
index index.html,index.htm;
try_files $uri $uri/ /index.html;
}
}
}
uwsgi_pass 127.0.0.1:8000; ##这里的端口需要与uwgsi配置文件中指定的端口一致
3.重启动nginx
[root@dsc1 nginx]# systemctl stop nginx
[root@dsc1 nginx]# systemctl start nginx
4.通过nginx代理的端口访问
http://192.168.1.102:8099/admin
5.创建账户
我们这里创建账户admin 密码也是admin 创建完成就可以登录了
[root@dsc1 djangohxl]#cd /deploy/djangohxl
[root@dsc1 djangohxl]# python manage.py createsuperuser
Username (leave blank to use 'root'): admin
Email address:
Password:
Password (again):
The password is too similar to the username.
This password is too short. It must contain at least 8 characters.
This password is too common.
Bypass password validation and create user anyway? [y/N]: y
Superuser created successfully.
#########################vue部署到服务器###############
1.打包vue项目
C:\Users\Administrator\PycharmProjects\myvue02>npm run build
把压缩包上传到服务器,然后解压
提取dist静态资源
将静态资源放置后端 /deploy/djangohxl/static/dist下
[root@dsc1 djangohxl]# cd /deploy/djangohxl/static
[root@dsc1 static]# rar x dist.rar
[root@dsc1 static]# ls
admin dist dist.rar
[root@dsc1 static]# cd dist
[root@dsc1 dist]# ls
index.html static
2.通过nginx反向代理访问vue
http://192.168.1.102:8888/myvue