• centos7下部署Django(nginx+uwsgi+python3+django)


    centos7下部署Django(nginx+uwsgi+python3+django)
    安装各类基础模块

    yum install gcc-c++
    yum install wget openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel
    yum install libxml*
    yum -y install zlib*
    yum remove lrzsz -y
    yum install lrzsz -y
    关闭防火墙
    systemctl stop firewalld
    1. yum install gcc-c++
    2. yum install wget openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel
    3. yum install libxml*
    4. yum -y install zlib*
    5. yum remove lrzsz -y
    6. yum install lrzsz -y
    7. 关闭防火墙
    8. systemctl stop firewalld
    yum install gcc-c++
    yum install wget openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel
    yum install libxml*
    yum -y install zlib*
    yum remove lrzsz -y
    yum install lrzsz -y
    关闭防火墙
    systemctl stop firewalld
     

    ———————————————————————————————-
    编译安装python3

    mkdir /application/
    cd /application/
    wget https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tgz
    tar xf Python-3.6.3.tgz -C /usr/local/src/
    cd /usr/local/src/Python-3.6.3/
    ./configure --prefix=/usr/local/python3
    make && make install
    将export PATH=/usr/local/python3/bin:$PATH粘贴到/etc/profile末尾
    source /etc/profile
    给python3安装django和uwsgi以及配置启动项目的xml文件
    注:如果出现一片红,那就多执行几遍
    pip3 install django
    pip3 install uwsgi
    查看安装了哪些包
    pip3 freeze or pip list
    挂软链接
    ln -s /usr/local/python3/bin/uwsgi /usr/bin/uwsgi3
    1. mkdir /application/
    2. cd /application/
    3. wget https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tgz
    4. tar xf Python-3.6.3.tgz -C /usr/local/src/
    5. cd /usr/local/src/Python-3.6.3/
    6. ./configure --prefix=/usr/local/python3
    7. make && make install
    8. 将export PATH=/usr/local/python3/bin:$PATH粘贴到/etc/profile末尾
    9. source /etc/profile
    10. 给python3安装django和uwsgi以及配置启动项目的xml文件
    11. 注:如果出现一片红,那就多执行几遍
    12. pip3 install django
    13. pip3 install uwsgi
    14. 查看安装了哪些包
    15. pip3 freeze or pip list
    16. 挂软链接
    17. ln -s /usr/local/python3/bin/uwsgi /usr/bin/uwsgi3
    mkdir /application/
    cd /application/
    wget https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tgz
    tar xf Python-3.6.3.tgz -C /usr/local/src/
    cd /usr/local/src/Python-3.6.3/
    ./configure --prefix=/usr/local/python3
    make && make install
    将export PATH=/usr/local/python3/bin:$PATH粘贴到/etc/profile末尾
    source /etc/profile
    给python3安装django和uwsgi以及配置启动项目的xml文件
    注:如果出现一片红,那就多执行几遍
    pip3 install django
    pip3 install uwsgi
    查看安装了哪些包
    pip3 freeze or pip list
    挂软链接
    ln -s /usr/local/python3/bin/uwsgi /usr/bin/uwsgi3
     

    ————————————————————————————————-
    uwsgi配置

    上传项目目录
    cd /application/
    mkdir 项目
    cd 项目,上传项目
    
    runserver测试
    python3 项目目录/manage.py runserver 10.0.0.8:80
    
    mkdir script
    cd script
    vim uwsgi.ini
    
    [uwsgi]
    # 项目目录
    chdir=/application/opt/yb
    # 启动uwsgi的用户名和用户组
    uid=root
    gid=root
    # 指定项目的application
    module=yb.wsgi:application
    # 指定sock的文件路径
    socket=/application/opt/script/uwsgi.sock
    # 启用主进程
    master=true
    # 进程个数
    workers=5
    pidfile=/application/opt/script/uwsgi.pid
    # 自动移除unix Socket和pid文件当服务停止的时候
    vacuum=true
    # 序列化接受的内容,如果可能的话
    thunder-lock=true
    # 启用线程
    enable-threads=true
    # 设置自中断时间
    harakiri=30
    # 设置缓冲
    post-buffering=4096
    # 设置日志目录
    daemonize=/application/opt/script/uwsgi.log
    ########################################
    启动uwsgi
    uwsgi --ini uwsgi.ini
    关闭uwsgi
    uwsgi --stop uwsgi.pid
    查看uwsgi日志
    tail -f /application/opt/script/uwsgi.log
    1. 上传项目目录
    2. cd /application/
    3. mkdir 项目
    4. cd 项目,上传项目
    5. runserver测试
    6. python3 项目目录/manage.py runserver 10.0.0.8:80
    7. mkdir script
    8. cd script
    9. vim uwsgi.ini
    10. [uwsgi]
    11. # 项目目录
    12. chdir=/application/opt/yb
    13. # 启动uwsgi的用户名和用户组
    14. uid=root
    15. gid=root
    16. # 指定项目的application
    17. module=yb.wsgi:application
    18. # 指定sock的文件路径
    19. socket=/application/opt/script/uwsgi.sock
    20. # 启用主进程
    21. master=true
    22. # 进程个数
    23. workers=5
    24. pidfile=/application/opt/script/uwsgi.pid
    25. # 自动移除unix Socket和pid文件当服务停止的时候
    26. vacuum=true
    27. # 序列化接受的内容,如果可能的话
    28. thunder-lock=true
    29. # 启用线程
    30. enable-threads=true
    31. # 设置自中断时间
    32. harakiri=30
    33. # 设置缓冲
    34. post-buffering=4096
    35. # 设置日志目录
    36. daemonize=/application/opt/script/uwsgi.log
    37. ########################################
    38. 启动uwsgi
    39. uwsgi --ini uwsgi.ini
    40. 关闭uwsgi
    41. uwsgi --stop uwsgi.pid
    42. 查看uwsgi日志
    43. tail -f /application/opt/script/uwsgi.log
    上传项目目录
    cd /application/
    mkdir 项目
    cd 项目,上传项目
    
    runserver测试
    python3 项目目录/manage.py runserver 10.0.0.8:80
    
    mkdir script
    cd script
    vim uwsgi.ini
    
    [uwsgi]
    # 项目目录
    chdir=/application/opt/yb
    # 启动uwsgi的用户名和用户组
    uid=root
    gid=root
    # 指定项目的application
    module=yb.wsgi:application
    # 指定sock的文件路径
    socket=/application/opt/script/uwsgi.sock
    # 启用主进程
    master=true
    # 进程个数
    workers=5
    pidfile=/application/opt/script/uwsgi.pid
    # 自动移除unix Socket和pid文件当服务停止的时候
    vacuum=true
    # 序列化接受的内容,如果可能的话
    thunder-lock=true
    # 启用线程
    enable-threads=true
    # 设置自中断时间
    harakiri=30
    # 设置缓冲
    post-buffering=4096
    # 设置日志目录
    daemonize=/application/opt/script/uwsgi.log
    ########################################
    启动uwsgi
    uwsgi --ini uwsgi.ini
    关闭uwsgi
    uwsgi --stop uwsgi.pid
    查看uwsgi日志
    tail -f /application/opt/script/uwsgi.log
     

    安装nginx

    yum install nginx
    /usr/sbin/nginx
    ps -ef |grep nginx
    1. yum install nginx
    2. /usr/sbin/nginx
    3. ps -ef |grep nginx
    yum install nginx
    /usr/sbin/nginx
    ps -ef |grep nginx
     

    nginx配置

    >/etc/nginx/nginx.conf
    mkdir /etc/nginx/logs
    touch /etc/nginx/logs/django.log
    touch /etc/nginx/logs/errordjango.log
    #nginx配置
    worker_processes  8;
    events {
        worker_connections  1024;
    }
    http {
        include       mime.types;
        default_type  application/octet-stream;
        sendfile        on;
        keepalive_timeout  65;
        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for"';
    #django配置				  
        server {
                        listen 80;
                        server_name dh.gcav.me;
                        access_log  /etc/nginx/logs/django.log;
                        error_log  /etc/nginx/logs/errordjango.log;
                        charset  utf-8;
                        gzip on;
                        error_page  404           /404.html;
                        error_page   500 502 503 504  /50x.html;
                        # 指定项目路径uwsgi
                        location / {
                            include uwsgi_params;
                            uwsgi_connect_timeout 30;
                            uwsgi_pass unix:/application/opt/script/uwsgi.sock;
                        }
                        # 指定静态文件路径
                        location /static/{
                            alias  /application/opt/yb/static/;
                            index  index.html index.htm;
                        }
      }
    #php配置
    #    server {
    #					listen       80;
    #					server_name  gcav.me;
    #					return       301 http://www.gcav.me$request_uri;
    #					index  index.php index.html index.htm;
    #    }
    #
    #    server {
    #					listen       80;
    #					server_name  www.gcav.me;
    #					access_log  /var/log/nginx/video.log;
    #					error_log  /var/log/nginx/videoerror.log;
    #					root   /usr/share/nginx/html/video;
    #					index  index.php index.html index.htm;
    #					location ~ .*.(php|php5)?$ {
    #						root /usr/share/nginx/html/video;
    #						fastcgi_pass  127.0.0.1:9000;
    #						fastcgi_index index.php;
    #						include fastcgi.conf;
    #        }   
    #    }
    #
    }
    1. >/etc/nginx/nginx.conf
    2. mkdir /etc/nginx/logs
    3. touch /etc/nginx/logs/django.log
    4. touch /etc/nginx/logs/errordjango.log
    5. #nginx配置
    6. worker_processes 8;
    7. events {
    8. worker_connections 1024;
    9. }
    10. http {
    11. include mime.types;
    12. default_type application/octet-stream;
    13. sendfile on;
    14. keepalive_timeout 65;
    15. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
    16. '$status $body_bytes_sent "$http_referer" '
    17. '"$http_user_agent" "$http_x_forwarded_for"';
    18. #django配置
    19. server {
    20. listen 80;
    21. server_name dh.gcav.me;
    22. access_log /etc/nginx/logs/django.log;
    23. error_log /etc/nginx/logs/errordjango.log;
    24. charset utf-8;
    25. gzip on;
    26. error_page 404 /404.html;
    27. error_page 500502503504 /50x.html;
    28. # 指定项目路径uwsgi
    29. location / {
    30. include uwsgi_params;
    31. uwsgi_connect_timeout 30;
    32. uwsgi_pass unix:/application/opt/script/uwsgi.sock;
    33. }
    34. # 指定静态文件路径
    35. location /static/{
    36. alias /application/opt/yb/static/;
    37. index index.html index.htm;
    38. }
    39. }
    40. #php配置
    41. # server {
    42. # listen 80;
    43. # server_name gcav.me;
    44. # return 301 http://www.gcav.me$request_uri;
    45. # index index.php index.html index.htm;
    46. # }
    47. #
    48. # server {
    49. # listen 80;
    50. # server_name www.gcav.me;
    51. # access_log /var/log/nginx/video.log;
    52. # error_log /var/log/nginx/videoerror.log;
    53. # root /usr/share/nginx/html/video;
    54. # index index.php index.html index.htm;
    55. # location ~ .*.(php|php5)?$ {
    56. # root /usr/share/nginx/html/video;
    57. # fastcgi_pass 127.0.0.1:9000;
    58. # fastcgi_index index.php;
    59. # include fastcgi.conf;
    60. # }
    61. # }
    62. #
    63. }
    >/etc/nginx/nginx.conf
    mkdir /etc/nginx/logs
    touch /etc/nginx/logs/django.log
    touch /etc/nginx/logs/errordjango.log
    #nginx配置
    worker_processes  8;
    events {
        worker_connections  1024;
    }
    http {
        include       mime.types;
        default_type  application/octet-stream;
        sendfile        on;
        keepalive_timeout  65;
        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for"';
    #django配置				  
        server {
                        listen 80;
                        server_name dh.gcav.me;
                        access_log  /etc/nginx/logs/django.log;
                        error_log  /etc/nginx/logs/errordjango.log;
                        charset  utf-8;
                        gzip on;
                        error_page  404           /404.html;
                        error_page   500 502 503 504  /50x.html;
                        # 指定项目路径uwsgi
                        location / {
                            include uwsgi_params;
                            uwsgi_connect_timeout 30;
                            uwsgi_pass unix:/application/opt/script/uwsgi.sock;
                        }
                        # 指定静态文件路径
                        location /static/{
                            alias  /application/opt/yb/static/;
                            index  index.html index.htm;
                        }
      }
    #php配置
    #    server {
    #					listen       80;
    #					server_name  gcav.me;
    #					return       301 http://www.gcav.me$request_uri;
    #					index  index.php index.html index.htm;
    #    }
    #
    #    server {
    #					listen       80;
    #					server_name  www.gcav.me;
    #					access_log  /var/log/nginx/video.log;
    #					error_log  /var/log/nginx/videoerror.log;
    #					root   /usr/share/nginx/html/video;
    #					index  index.php index.html index.htm;
    #					location ~ .*.(php|php5)?$ {
    #						root /usr/share/nginx/html/video;
    #						fastcgi_pass  127.0.0.1:9000;
    #						fastcgi_index index.php;
    #						include fastcgi.conf;
    #        }   
    #    }
    #
    }
     

    修改静态文件路径

    1.将项目的settings.py文件底部添加
    STATIC_ROOT = os.path.join(BASE_DIR, "static_all")
    2.执行命令生效
    python3 manage.py collectstatic
    3.修改nginx.conf静态文件路径
                        # 指定静态文件路径
                        location /static/{
                            alias  /application/opt/yb/static_all/;
                            index  index.html index.htm;
                        }
    4.重启服务
    1. 1.将项目的settings.py文件底部添加
    2. STATIC_ROOT = os.path.join(BASE_DIR, "static_all")
    3. 2.执行命令生效
    4. python3 manage.py collectstatic
    5. 3.修改nginx.conf静态文件路径
    6. # 指定静态文件路径
    7. location /static/{
    8. alias /application/opt/yb/static_all/;
    9. index index.html index.htm;
    10. }
    11. 4.重启服务
    1.将项目的settings.py文件底部添加
    STATIC_ROOT = os.path.join(BASE_DIR, "static_all")
    2.执行命令生效
    python3 manage.py collectstatic
    3.修改nginx.conf静态文件路径
                        # 指定静态文件路径
                        location /static/{
                            alias  /application/opt/yb/static_all/;
                            index  index.html index.htm;
                        }
    4.重启服务
     

    启动nginx

    /usr/sbin/nginx -t
    /usr/sbin/nginx
    1. /usr/sbin/nginx -t
    2. /usr/sbin/nginx
    /usr/sbin/nginx -t
    /usr/sbin/nginx
     

    查看nginx日志

    tail -f /etc/nginx/logs/django.log
    1. tail -f /etc/nginx/logs/django.log
    tail -f /etc/nginx/logs/django.log
     

    关闭uwsgi和nginx

    uwsgi --stop uwsgi.pid
    /usr/sbin/nginx -s stop
    1. uwsgi --stop uwsgi.pid
    2. /usr/sbin/nginx -s stop
    uwsgi --stop uwsgi.pid
    /usr/sbin/nginx -s stop
     

    创建admin

    cd 到项目目录下创建admin
    python3 manage.py migrate
    创建admin超级用户
    python3 manage.py createsuperuser
    python3 manage.py collectstatic --noinput
    1. cd 到项目目录下创建admin
    2. python3 manage.py migrate
    3. 创建admin超级用户
    4. python3 manage.py createsuperuser
    5. python3 manage.py collectstatic --noinput
    cd 到项目目录下创建admin
    python3 manage.py migrate
    创建admin超级用户
    python3 manage.py createsuperuser
    python3 manage.py collectstatic --noinput
     

    连接数据库

    编辑app目录下__init__.py文件
    import pymysql 
    pymysql.install_as_MySQLdb()
    
    修改settings.py文件中的DATABASES
    django.db.backends.postgresql 连接 PostgreSQL
    
    django.db.backends.mysql 连接 mysql
    
    django.db.backends.sqlite3 连接 sqlite
    
    django.db.backends.oracle 连接 oracle
    若是要连接mysql之类的,需要账户密码的,连接配置应该这样写:
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'NAME': '库名',
            'USER': '用户名',
            'PASSWORD': '密码',
            'HOST': '127.0.0.1',
            'PORT': '3306',
        }
    }
    第一个命令:创建迁移文件
    python3 manage.py makemigrations
    第二个命令:同步到数据库
    python3 manage.py migrate
    1. 编辑app目录下__init__.py文件
    2. import pymysql
    3. pymysql.install_as_MySQLdb()
    4. 修改settings.py文件中的DATABASES
    5. django.db.backends.postgresql 连接 PostgreSQL
    6. django.db.backends.mysql 连接 mysql
    7. django.db.backends.sqlite3 连接 sqlite
    8. django.db.backends.oracle 连接 oracle
    9. 若是要连接mysql之类的,需要账户密码的,连接配置应该这样写:
    10. DATABASES = {
    11. 'default': {
    12. 'ENGINE': 'django.db.backends.mysql',
    13. 'NAME': '库名',
    14. 'USER': '用户名',
    15. 'PASSWORD': '密码',
    16. 'HOST': '127.0.0.1',
    17. 'PORT': '3306',
    18. }
    19. }
    20. 第一个命令:创建迁移文件
    21. python3 manage.py makemigrations
    22. 第二个命令:同步到数据库
    23. python3 manage.py migrate
    编辑app目录下__init__.py文件
    import pymysql 
    pymysql.install_as_MySQLdb()
    
    修改settings.py文件中的DATABASES
    django.db.backends.postgresql 连接 PostgreSQL
    
    django.db.backends.mysql 连接 mysql
    
    django.db.backends.sqlite3 连接 sqlite
    
    django.db.backends.oracle 连接 oracle
    若是要连接mysql之类的,需要账户密码的,连接配置应该这样写:
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'NAME': '库名',
            'USER': '用户名',
            'PASSWORD': '密码',
            'HOST': '127.0.0.1',
            'PORT': '3306',
        }
    }
    第一个命令:创建迁移文件
    python3 manage.py makemigrations
    第二个命令:同步到数据库
    python3 manage.py migrate
     

    更改数据库编码为utf8

    mysql.cnf存在于mysql的安装目录下或/etc/下在my.cnf或my.ini下找到[mysqld], 在其下方添加一行:
    character_set_server=utf8
    然后保存退出, 并重启mysql服务即可。
    
    1. mysql.cnf存在于mysql的安装目录下或/etc/下在my.cnf或my.ini下找到[mysqld], 在其下方添加一行:
    2. character_set_server=utf8
    3. 然后保存退出, 并重启mysql服务即可。
    mysql.cnf存在于mysql的安装目录下或/etc/下在my.cnf或my.ini下找到[mysqld], 在其下方添加一行:
    character_set_server=utf8
    然后保存退出, 并重启mysql服务即可。
     
     
  • 相关阅读:
    MySQL表操作:字段类型 约束条件
    MySQL安装 sql语句
    O学堂作文
    iOS沙盒
    xcode 8带来的问题
    身份证号验证(省份,生日,末位校验)
    UIView的layoutSubviews和drawRect方法何时调用
    UIView超出父视图部分响应
    xcode 8带来的问题
    计算今天是周几
  • 原文地址:https://www.cnblogs.com/xiao-xue-di/p/12911670.html
Copyright © 2020-2023  润新知