如何部署djiango项目
1.安装python3.9.9
下载 Python 源码
去官方网站下载:
wget https://www.python.org/ftp/python/3.9.9/Python-3.9.9.tgz
官网的下载速度实在感人,换成阿里的镜像:
wget https://cdn.npm.taobao.org/dist/python/3.9.9/Python-3.9.9.tgz
第3步:解压
tar -xzf Python-3.9.9.tgz
第4步:配置安装选项
./configure
第5步:编译、安装
make
make install
第6步:设置软连接
lwf@lianan:~$ which python3.9
/usr/local/bin/python3.9
lwf@lianan:~$ ln -s /usr/local/bin/python3.9 python3.9
2 2、安装Django4.2.1
使用以下命令:
sudo apt-get update
pip3 install django==4.2.1 -i https://pypi.doubanio.com/simple/
验证是否安装成功:
python3
import django
django.get_version()
安装nginx
sudo apt-get install nginx
安装uwsgi
pip3 install uwsgi -i https://pypi.doubanio.com/simple/(如果此命令装不成功,使用以下命令)
sudo apt-get install uwsgi
6、配置项目
1通过传输工具将项目传到/home/wwwroot/下
在上传之前需要,先创建wwwroot目录并设置755权限
2.配置nginx
cd /etc/nginx/sites-available
vim default
找到default文件下的这段代码,如果没有就要添加上去:
server_name 111.*.*.124; ##改成你的公网ip
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
#try_files $uri $uri/ =404;
include uwsgi_params;
uwsgi_pass 127.0.0.1:8000; #和uwsgi中端口配置保持一致
}
location /static {
alias /home/www/***/static; #项目的static目录
}
location /media {
alias /home/www/**;
}
static 那里也要替换成你自己Django项目的static路径;
media 同理换成你自己Django项目的media路径,这是媒体文件的文件夹;
注意:location后面是有空格的,必须要有!alias后面也是有空格的;include上面那句话也是要注释掉的!
修改完成后保存退出,然后重启nginx服务器
sudo service nginx restart
后面踩一个坑,这边先记录一下。就是静态文件无法访问,浏览器检查源代码直接访问css文件,出现403错误!直接把static目录设置777权限还是不行,折腾了半天之后才发现是用户许可问题。
修改nginx配置文件/etc/nginx/nginx.conf ,user www 改成user root 或者你的用户名
3)、配置uwsgi
在你的django项目的根目录下,也就是有manage.py文件的目录下,新建一个uwsgi.ini文件和一个run.log文件
然后我们使用vim编辑器编辑uwsgi.ini文件,输入以下代码:
[uwsgi]
chdir = /home/wwwroot/xcx
wsgi-file = xcx/wsgi.py
module = xcx_back.wsgi:application
socket = 127.0.0.1:8000
master = true
pidfile = uwsgi.pid
daemonize = /home/wwwroot/xcx/run.log
disable-logging = true
~
chdir 是你的django项目的根目录,替换成你自己的路径;
moudule 是你的入口wsgi模块,替换成你自己的项目名称;
socket 是通信端口设置,这个设置不用改,复制即可;
master = true 表示以主进程模式运行,不用改,复制即可;
daemonize 是你的日志文件目录,这个路径就是刚刚新建的run.log文件的路径;
disable-logging = true 表示不记录正常信息,只记录错误信息,否则你的日志可能很快就爆满了!
问题: 在启动nginx会报如下错误
Job for nginx.service failed because the control process exited with error code.
See "systemctl status nginx.service" and "journalctl -xe" for details.
解决方案:
1.查看防火墙启动的端口号
[root@iZuf68mr29sh8zy1elsaixZ ~]# firewall-cmd --list-ports
80/tcp 3306/tcp
2、重启防火墙
[root@iZuf68mr29sh8zy1elsaixZ ~]# firewall-cmd --reload
success
3.重启nginx,出现如下错误
Job for nginx.service failed because the control process exited with error code.
See "systemctl status nginx.service" and "journalctl -xe" for details.
4、查看nginx服务
[root@iZuf68mr29sh8zy1elsaixZ ~]# systemctl status nginx.service
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Sun 2023-02-05 11:02:23 CST; 36s ago
Process: 1416 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=1/FAILURE)
Process: 1414 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
Feb 05 11:02:23 iZuf68mr29sh8zy1elsaixZ systemd[1]: Starting The nginx HTTP an...
Feb 05 11:02:23 iZuf68mr29sh8zy1elsaixZ nginx[1416]: nginx: the configuration ...
Feb 05 11:02:23 iZuf68mr29sh8zy1elsaixZ nginx[1416]: nginx: [emerg] bind() to ...
Feb 05 11:02:23 iZuf68mr29sh8zy1elsaixZ nginx[1416]: nginx: configuration file...
Feb 05 11:02:23 iZuf68mr29sh8zy1elsaixZ systemd[1]: nginx.service: control pro...
Feb 05 11:02:23 iZuf68mr29sh8zy1elsaixZ systemd[1]: Failed to start The nginx ...
Feb 05 11:02:23 iZuf68mr29sh8zy1elsaixZ systemd[1]: Unit nginx.service entered...
Feb 05 11:02:23 iZuf68mr29sh8zy1elsaixZ systemd[1]: nginx.service failed.
Hint: Some lines were ellipsized, use -l to show in full.
可以看出修改完配置信息后没有提交:
[root@iZuf68mr29sh8zy1elsaixZ tmp]# nginx -t -c /etc/nginx/nginx.conf
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: [emerg] bind() to 139.196.234.86:80 failed (99: Cannot assign requested a ddress)
结果发现80端口被占用
[root@iZuf68mr29sh8zy1elsaixZ tmp]# netstat -natp |grep 80
tcp 0 0 172.16.85.16:43064 100.100.30.26:80 ESTABLISHED 1189/AliYunDun
将80端口杀死:
[root@iZuf68mr29sh8zy1elsaixZ tmp]# kill 1189
[root@iZuf68mr29sh8zy1elsaixZ tmp]# netstat -natp |grep 80
tcp 0 0 172.16.85.16:43064 100.100.30.26:80 TIME_WAIT -
5.测试nginx服务
[root@iZuf68mr29sh8zy1elsaixZ tmp]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
测试成功
重新加载配置文件:
[root@iZuf68mr29sh8zy1elsaixZ tmp]# nginx -s reload
nginx: [error] invalid PID number "" in "/run/nginx.pid"
出现错误
重启:
[root@iZuf68mr29sh8zy1elsaixZ tmp]# nginx -c /etc/nginx/nginx.conf
nginx: [emerg] bind() to 139.196.234.86:80 failed (98: Address already in use)
nginx: [emerg] bind() to 139.196.234.86:8000 failed (98: Address already in use)
nginx: [emerg] bind() to 139.196.234.86:80 failed (98: Address already in use)
nginx: [emerg] bind() to 139.196.234.86:8000 failed (98: Address already in use)
nginx: [emerg] bind() to 139.196.234.86:80 failed (98: Address already in use)
nginx: [emerg] bind() to 139.196.234.86:8000 failed (98: Address already in use)
nginx: [emerg] bind() to 139.196.234.86:80 failed (98: Address already in use)
nginx: [emerg] bind() to 139.196.234.86:8000 failed (98: Address already in use)
nginx: [emerg] bind() to 139.196.234.86:80 failed (98: Address already in use)
nginx: [emerg] bind() to 139.196.234.86:8000 failed (98: Address already in use)
nginx: [emerg] still could not bind()
发现报了如此对的错误信息
此时将nginx的所有进程都杀死:
killall nginx
重新启动nginx,就可以正常运行了
[root@iZuf68mr29sh8zy1elsaixZ tmp]# service nginx start
Starting nginx (via systemctl): [ OK ]
[root@iZuf68mr29sh8zy1elsaixZ tmp]# systemctl status nginx.service
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: active (running) since Sun 2023-02-05 11:20:16 CST; 40s ago
Process: 1763 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
Process: 1760 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
Process: 1756 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
Main PID: 1764 (nginx)
CGroup: /system.slice/nginx.service
├─1764 nginx: master process /usr/sbin/nginx
├─1765 nginx: worker process
└─1766 nginx: worker process
Feb 05 11:20:16 iZuf68mr29sh8zy1elsaixZ systemd[1]: Starting The nginx HTTP and reverse proxy server...
Feb 05 11:20:16 iZuf68mr29sh8zy1elsaixZ nginx[1760]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Feb 05 11:20:16 iZuf68mr29sh8zy1elsaixZ nginx[1760]: nginx: configuration file /etc/nginx/nginx.conf test is s...sful
Feb 05 11:20:16 iZuf68mr29sh8zy1elsaixZ systemd[1]: Started The nginx HTTP and reverse proxy server.
Hint: Some lines were ellipsized, use -l to show in full.