Django 部署到服务器

1:安装Python以及mysql相关依赖
sudo apt-get update && sudo apt-get upgrade
sudo apt-get install -y build-essential checkinstall libreadline-gplv2-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev
sudo apt-get dist-upgrade
sudo apt-get install -y build-essential python-dev python-setuptools python-pip python-smbus
sudo apt-get install -y build-essential libncursesw5-dev libgdbm-dev libc6-dev zlib1g-dev libsqlite3-dev tk-dev libssl-dev openssl libffi-dev wget

sudo apt-get install pkg-config
sudo apt-get install python3-dev default-libmysqlclient-dev

2:下载python源码,以python3.9.8版本为例
wget https://www.python.org/ftp/python/3.9.8/Python-3.9.8.tgz
解压到当前目录:tar zxvf Python-3.9.8.tgz
cd到源码目录,配置编译环境开始编译
./configure
./make
./make install
查看python版本:python3 --version
如果显示的还是旧版本,需要删除之前的pyhon3软链接
cd /usr/bin
ls -l python*
lrwxrwxrwx 1 root root 9 4月 16 2018 python -> python2.7
lrwxrwxrwx 1 root root 9 4月 16 2018 python2 -> python2.7
-rwxr-xr-x 1 root root 3637096 3月 9 2023 python2.7
lrwxrwxrwx 1 root root 33 3月 9 2023 python2.7-config -> x86_64-linux-gnu-python2.7-config
lrwxrwxrwx 1 root root 16 4月 16 2018 python2-config -> python2.7-config
lrwxrwxrwx 1 root root 9 4月 16 2022 python3 -> python3.6
-rwxr-xr-x 2 root root 4526456 3月 11 2023 python3.6
lrwxrwxrwx 1 root root 33 3月 11 2023 python3.6-config -> x86_64-linux-gnu-python3.6-config
-rwxr-xr-x 2 root root 4526456 3月 11 2023 python3.6m
lrwxrwxrwx 1 root root 34 3月 11 2023 python3.6m-config -> x86_64-linux-gnu-python3.6m-config
lrwxrwxrwx 1 root root 16 10月 25 2018 python3-config -> python3.6-config
lrwxrwxrwx 1 root root 10 4月 16 2022 python3m -> python3.6m
lrwxrwxrwx 1 root root 17 10月 25 2018 python3m-config -> python3.6m-config
lrwxrwxrwx 1 root root 16 4月 16 2018 python-config -> python2.7-config

可以看到pyhon3指向的还是python3.6版本
删除再创建
rm python3
ln -s /usr/local/bin/python3.9 /usr/bin/python3
查看pip3版本
pip3 --version
pip 21.2.4 from /usr/local/lib/python3.9/site-packages/pip (python 3.9)

3:安装virtualenv创建虚拟环境
cd /data
virtualenv venv
这里遇到个报错:
File "/usr/local/lib/python3.8/subprocess.py", line 512, in run raise CalledProcessError(retcode, process.args, subprocess.CalledProcessError: Command '('lsb_release', '-a')' returned non-zero exit status 1.
定位到最终的错误为 lsb_release

打开/usr/bin/lsb_release,修改第一行

原第一行代码如下 #!/usr/bin/python3.8 -Es
修改为如下 #!/usr/bin/python3.9 -ES
保存后错误消除

4:激活虚拟环境
source /data/venv/bin/activate
安装项目依赖的库
pip3 install -r requirements.txt

requirements.txt是在开发环境下生成的
pip3 freeze > requirements.txt
同步到服务器即可

5:安装配置uwsgi
pip3 install uwsgi
安装成功后显示
Successfully built uwsgi
Installing collected packages: uwsgi
Successfully installed uwsgi-2.0.24

修改Django项目中setting.py以下两处
DEBUG = False
ALLOWED_HOSTS = [*]

增加配置
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

在项目根目录下创建uwssi.ini文件
cd /data/www/blog_demo
touch uwsgi.ini
vim uwsgi.ini,以下为示例
# uwsgi.ini file
[uwsgi]

# Django-related settings

socket = 127.0.0.1:8000

# the base directory (full path)
chdir = /data/www/blog_demo

# Django s wsgi file
wsgi-file = %(chdir)/blog_demo/wsgi.py

# process-related settings
master = true

# maximum number of worker processes
processes = 1

#maximum number of worker threads
threads = 2

max-requests = 1000

chmod-socket = 660

# try to remove all of the generated file/sockets
vacuum = true

#本地文件修改后自动重新加载
py-autoreload = 1

virtualenv = /data/venv

pidfile = %(chdir)/uwsgi.pid

stats = %(chdir)/uwsgi.status

logto = %(chdir)/logs/error.log

daemonize = %(chdir)/uwsgi.log

buffer-size = 655365

以上内容根据实际项目配置

uwsgi启动和停止
uwsgi --ini uwsgi.ini
uwsgi --stop uwsgi.pid

ps -ef | grep wsgi
android 30036 1 2 14:40 ? 00:00:00 uwsgi --ini uwsgi.ini
android 30037 30036 0 14:40 ? 00:00:00 uwsgi --ini uwsgi.ini
android 30038 30036 0 14:40 ? 00:00:00 uwsgi --ini uwsgi.ini

说明uwsgi运行成功,再看看本地能不能访问
curl http://127.0.0.1:8000
无报错信息说明uwsgi部署成功,否则需要根据uwsgi.log分析错误原因

6:安装配置nginx
sudo apt-get install nginx
查看nginx版本
nginx -v
nginx version: nginx/1.14.0 (Ubuntu)

最好先备份/etc/nginx/nginx.conf
在http节点末尾增加以下配置:
server {

            listen 80;
            server_name xxx.xxx.xxx.xxx; //这里写服务器的公网ip或域名

            location / {
                    uwsgi_pass 127.0.0.1:8000; //对应uwsgi的配置
                    include uwsgi_params;
            }

            location /static {
                    alias /data/www/blog_demo/static/; //项目的静态文件目录
            }
    }

可以用nginx -t查看配置有没有错误
nginx: [warn] the "user" directive makes sense only if the master process runs with super-user privileges, ignored in /etc/nginx/nginx.conf:1
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok

nginx 启动命令
service nginx start
service nginx restart
service nginx stop

如果nginx启动成功但无法访问80端口,可以通过sudo netstat -tuln 查看系统端口开放状态

posted @   ZhengYuheng  阅读(204)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
点击右上角即可分享
微信分享提示