<返回更多

Django项目部署到服务器

2021-10-13    黄小墨,墨迹的墨
加入收藏

1. 安装Python/ target=_blank class=infotextkey>Python3.8

1.1 安装相关依赖包

yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel libffi-devel

项目python版本为3.8.5, django版本为3.1.1

1.2 下载并解压python3.8.5

yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel libffi-devel

1.3 编译安装

cd Python-3.8.5/./configure --prefix=/usr/local/python3makemake install

1.4 创建软链接

ln -s /usr/local/python3/bin/python3.8 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3.8 /usr/bin/pip3

2. 安装项目相关库

2.1 项目中所使用的库可以导出到文本文件中

# 从开发环境中导出所安装的库到requirements.txt文件中

pip freeze > requirements.txt

2.2 在服务器中安装requirements.txt中的库

pip install -r requirements.txt

2.3 安装uwsgi

pip install uwsgi

3. 安装MySQL

3.1 下载

wget http://tel.mirrors.163.com/mysql/Downloads/MySQL-5.7/mysql-5.7.34-el7-x86_64.tar.gz

3.2 拷贝安装包到/usr/local/目录下,并解压

# 重命名压缩包

mv mysql-5.7.34-el7-x86_64.tar.gz mysql.tar.gztar zxf mysql.tar.gz
# 进入mysql目录
cd mysql

3.3 创建data目录

cd mysqlmkdir data

3.4 创建mysql用户并设置权限

useradd mysql -s /sbin/nologin -M
# 回到/usr/local目录cd ..
# 设置mysql目录权限
chown -R mysql.mysql mysql

3.5 初始化数据库

cd mysql
# 执行后,屏幕会提示root临时密码,需要记录下来
bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data

bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory

yum install libaio

3.6 编辑my.cnf文件

 

wget https://www.python.org/ftp/python/3.8.5/Python-3.8.5.tgztar zvxf Python-3.8.5.tgz

3.7 创建error.log

cd Python-3.8.5/./configure --prefix=/usr/local/python3makemake install

3.8 拷贝启动文件到/etc/init.d目录

ln -s /usr/local/python3/bin/python3.8 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3.8 /usr/bin/pip3

3.8.1 修改启动文件中的路径

# vim /etc/init.d/mysqld
# 修改basedir和datadir
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data

3.9 启动mysql

/etc/init.d/mysqld start

3.10 创建软链接

ln -s /usr/local/mysql/mysql.sock /tmp/mysql.sock

3.11 使用临时密码登录数据库并重新为root设置密码

cd /usr/local/mysql
# 回车后会提示输入密码,输入临时密码回车即可
bin/mysql -uroot -p

# 登录之后修改密码
mysql> set password = password('123456');
mysql> alter user 'root'@'localhost' password expire never;
mysql> flush privileges;

4. 安装Nginx

4.1 下载

wget http://nginx.org/download/nginx-1.17.7.tar.gz

4.2 安装相关依赖包

yum install -y pcre pcre-devel openssl openssl-devel gcc

4.3 创建nginx用户

useradd nginx -s /sbin/nologin -M

4.4 安装nginx

tar zxf nginx-1.17.7.tar.gz
cd nginx-1.17.7
./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
make && make install

5. 安装redis

5.1 安装

wget http://download.redis.io/releases/redis-2.8.17.tar.gz
tar xzf redis-2.8.17.tar.gz
mv redis-2.8.17 redis
cd redis
make 

5.2 启动

cd src
./redis.server

cd src
./redis.server
Django项目部署到服务器

 

6. uwsgi配置

# 在项目根目录下创建uwsgi.ini文件

[uwsgi]
# 配置nginx连接的socket连接 /data/web/project为项目目录
socket=/data/web/project/project.sock
# 配置uwsgi.pid所在的目录
pidfile=/data/web/project/uwsgi.pid
# 进程数
processes=8
# 每个进程的线程数
threads=2
# 项目路径,项目所在的目录
chdir=/data/web/project
# wsgi接口模块文件路径
module=project.wsgi
# 启动管理主进程
master=true
enable-threads=true
logdate=true
limit-as=6048
# 日志文件
daemonize=/data/web/project/run.log

6.1 启动

# -d表示后台启动
uwsgi -d --ini uwsgi.ini

6.2 相关操作

# 停止
uwsgi --stop uwsgi.pid
# 重启
uwsgi --reload uwsgi.pid

7. 配置nginx

7.1 nginx.conf

#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type Application/octet-stream;
#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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
include vhost/*.conf;
}

7.2 创建vhost目录

mkdir vhost
cd vhost

7.3 项目配置文件

# vim www.xxx.com.conf
server {
    listen 80;
    server_name www.xxx.com;
    charset utf-8;
    client_max_body_size 50M;
  
    location / {
        uwsgi_pass unix:/data/web/project/project.sock;
        include uwsgi_params;
    }
  
    location /static {
       alias /data/web/project/static;
    }
  
    location /media {
      alias /data/web/project/media;
    }
}

8. 关于css, js 丢失问题

代码迁移至服务器前,使用命令 python manage.py collectstatic 收集静态文件

python manage.py collectstatic
声明:本站部分内容来自互联网,如有版权侵犯或其他问题请与我们联系,我们将立即删除或处理。
▍相关推荐
更多资讯 >>>