美文网首页
服务器部署Django项目

服务器部署Django项目

作者: Hahn_z | 来源:发表于2020-02-28 02:17 被阅读0次

我这里的服务器是阿里云centerOS 8.0

1. 环境配置

更新环境

# 如果不是root用户在命令前面加sudo
yum update
yum upgrade
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel xz-devel

安装git

yum install git

2. 安装python

安装pyenv
是用来python版本的工具

git clone https://github.com/pyenv/pyenv.git ~/.pyenv
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >>~/.bash_profile 
echo 'eval "$(pyenv init -)"' >>~/.bash_profile 
exec $SHELL -l #即是启动语句,重启系统执行这条语句

安装python 下面以python 3.7.4为例

 pyenv install --list # 查看可以安装的版本

安装指定版本

pyenv install 3.7.4 # pyenv install 指定版本 

如果pyenv install 没有反应 请去官网下载您需要的版本
在/root/.pyenv/ 下面新建cache文件夹

1.png

把下载的资源上传到cache目录下面

pyenv install -v 3.7.4

出现这个错误ModuleNotFoundError: No module named '_ctypes'

yum install libffi-devel 

继续安装

pyenv rehash # 刷新数据库
pyenv version #查看现在的python版本
pyenv global 3.7.4 #设置系统默认python版本
pyenv version # 显示 3.7.4 设置成功

3. 安装MySQL

我这里安装是默认最新的MySQL 现在是8.0.17

yum install -y mysql-server # 下载安装MySQL
service mysqld start # 启动MySQL

登录MySQL
注意一开始MySQL密码是空的

mysql -u root -p #启动了说明MySQL安装成功

使用Navicat
使用ssh登录

2.png

点击上方用户
点击root用户修改他的密码

3.png 4.png

编辑您的链接
输入完成就可以登录了

5.png

4.安装虚拟环境

我这里虚拟环境是pipenv

pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple pipenv
pipenv shell # 你要在你需要目录中启动虚拟环境
exit # 退出虚拟环境

5.上传Django项目

通过pycharm sftp上传项目

这篇文章详情讲了怎么上传

在本地对应项目文件夹(把需要库生成txt)、这里把文件放在manage.py同级

pip freeze > requirements.txt

在服务器中的虚拟环境项目目录里面 、会自动安装你Django里面的库

pip install -r requirements.txt 

在你的虚拟环境中的项目目录里面

python manage.py runserver 0.0.0.0:8000 # 这个目录在关闭ssh就不能访问了
nohup python manage.py runserver 0.0.0.0:8000 #这样关闭了ssh还可以运行

6.安装uwsgi

安装uwsgi 不要在虚拟环境中安装uWSGI

pip install uwsgi
ln -s /usr/local/python3/bin/uwsgi /usr/bin/uwsgi #软连接

测试是否安装成功

# 随便新建一个目录新建 test.py 写入如下内容
def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return [b"Hello World"]
# 在你新建的目录的下面 注意http :8000是你服务器需要开放的端口、不然服务器是无法访问的
uwsgi uwsgi --http :8000 --wsgi-file test.py

打开浏览器ip:8000 是否可以访问(8000是你Django运行端口)
这里我遇到一个坑,我本地测试可以,但是服务器无法访问,阿里云安全组没有开放对应端口

1.png

测试成功了
在pycharm和manage.py平级目录新建uwsgi.ini

;# uwsgi.ini
;[uwsgi]
;#django 端口号
;#http= ip:port
;socket= 127.0.0.1:8000
;master = true         //主进程
;#vhost = true          //多站模式
;#no-site = true        //多站模式时不设置入口模块和文件
;#workers = 2           //子进程数
;reload-mercy = 10
;vacuum = true         //退出、重启时清理文件
;max-requests = 1000
;limit-as = 512
;buffer-size = 30000
;
;#django 目录
;chdir=/data/mxonline_django2
;
;#Django 项目下 wsgi.py 文件路径
;wsgi-file=mxonline_django2/wsgi.py
;
;#进程数
;processes=2
;#服务器进程开启的县成熟
;threads=4
;
;pidfile=uwsgi.pid


# 这句代码必须加上
[uwsgi]
#配置和nginx连接的socket连接
socket=127.0.0.1:9000
#直接做web服务器使用
http=0.0.0.0:8000
#配置项目路径,项目的所在目录
chdir=您的项目路径
#配置wsgi接口模块文件路径
wsgi-file=您的项目路径/项目名字/wsgi.py
#配置启动的进程数
processes=4
#配置每个进程的线程数
threads=2
#配置启动管理主进程
master=True

stats = 您的项目静态文件
#配置存放主进程的进程号文件
pidfile=您的项目路径/项目名字/wsgi.py/uwsgi.pid
#配置dump日志记录
daemonize=您的项目路径/项目名字/uwsgi.log

你的项目的目录 是和manage.py平级目录 启动uwsgi

uwsgi --ini uwsgi.ini

打开浏览器ip:8000 访问(8000是你Django运行端口)
如果静态资源没有加载

静态资源没有加载解决办法

说一下遇到的坑

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, '/static/')
STATICFILES_DIRS = (
     os.path.join(BASE_DIR, 'static'),
)
直接写static就可以了、不用写项目的静态文件的路径

重新启动uwsgi

uwsgi --reload uwsgi.pid

打开浏览器ip:8000 访问

8.安装Nignx

sudo yum install -y nginx
systemctl start nginx.service # 启动Nginx

ginx的配置文件一般放在/usr/local/nginx/conf,/etc/nginx或 /usr/local/etc/nginx中,默认文件名为nginx.conf。
我这里的目录是在/etc/nginx

先测试一下nignx

vim /etc/nginx/nginx.conf

编辑配置文件
新建/home/my、在里面写一个index.html、和随便放点资源

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user root;  # 使用着修改成为root 不然等一下测试访问资源的时候会403
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 2048;

    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       80 default_server;
        listen       [::]:80 default_server;
        server_name  _; #你的域名
        root         /home/my; #我这里在/home/新建的目录 根目录

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

     location /media/ {
                alias /home/my/media/; #视频文件的目录
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers PROFILE=SYSTEM;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

}

重启nignx

systemctl restart nginx.service

输入IP地址访问
可以访问、配置成功

下面是uwsgi和nignx配置

vim /etc/nginx/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 root;
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 2048;

    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;
    

    #upstream django {
    #    # server unix:///path/to/your/mysite/mysite.sock; # for a file socket
    #    server 127.0.0.1:8000; # for a web port socket (we'll use this first)
    #}

    server {
        listen       80 default_server; # 监听80端口
        server_name  localhost; #域名 IP地址
        charset utf-8; 

        include /etc/nginx/default.d/*.conf;
    
    #client_max_body_size 75M;

        location / {
            proxy_pass http://127.0.0.1:8000; # uwsgi和nignx是http方式连接、要和uwsgi.ini里面的端口一样
            include /etc/nginx/uwsgi_params;
        #uwsgi_pass 127.0.0.1:9000; # uwsgi和nignx是scoket方式连接、要和uwsgi.ini里面的端口一样
        }
    
        location /static/ {
            alias /static/; # 静态文件 和Django里面的一样
        }
    
        #location /media/ {
        #    alias /data/mxonline/media;
        #}
    
        #error_page 404 /404.html;
        #    location = /40x.html {
        #}
    
        #error_page 500 502 503 504 /50x.html;
        #    location = /50x.html {
        #}
    }

# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers PROFILE=SYSTEM;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

}


重启nignx

systemctl restart nginx.service

输入IP地址访问
可以访问、配置成功

相关文章

网友评论

      本文标题:服务器部署Django项目

      本文链接:https://www.haomeiwen.com/subject/mwnqhhtx.html