美文网首页
从零开发搭建部署Django+Nginx+Gunicorn网站

从零开发搭建部署Django+Nginx+Gunicorn网站

作者: 349ff5da91d8 | 来源:发表于2018-12-04 16:49 被阅读33次

    示例网站(已上线):www.17mtd.com
    项目git地址:https://github.com/keenking/new

    (一)安装python+django
    安装依赖包--->下载最新版python源码包并解压--->配置编译--->删除编译python时所需的库--->链接到新的python版本

    1、安装依赖包:
    yum groupinstall "Development tools"
    yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel

    2、下载最新python版本python3.6.5:
    wget https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tgz

    3、解压下载包:
    tar xf Python-3.6.5.tgz

    4、配置并编译:
    ./configure

    5、编译并安装:
    make && make install

    6、删除库(可选):
    yum groupremove "Development tools"
    yum remove zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel

    7、链接python:
    将/usr/bin/目录下的python文件备份后删除,一定要删!不删会报错
    mv /usr/bin/python /usr/bin/python.bak
    链接到python3.6.5
    ln –s /usr/local/bin/python3 /usr/bin/python
    ln –s /usr/local/bin/pip3 /usr/bin/pip

    注意事项:yum可能会无法使用,vim /usr/bin/yum,将第一行的python后面加上2.7即可

    vim /usr/bin/firewall-cmd, 将#!/usr/bin/python -Es 改为 #!/usr/bin/python2.7 -Es
    vim /usr/sbin/firewalld, 将#!/usr/bin/python -Es 改为 #!/usr/bin/python2.7 -Es (这一步是针对于防火墙报错,进行的修改)

    cat /usr/libexec/urlgrabber-ext-down看了下,发下他也使用了/usr/bin/python,于是跟前面一样,改为2.7,即可

    安装pip:
    yum -y install epel-release
    yum install python-pip

    升级pip:
    pip install --upgrade pip

    安装django:
    pip install django
    (二)安装mysql
    安装mysql

    下载mysql源安装包

    wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm

    安装mysql源

    yum localinstall mysql57-community-release-el7-8.noarch.rpm
    yum install mysql-devel

    安装MySQL

    yum install mysql-community-server

    启动MySQL服务

    systemctl start mysqld

    查看MySQL的启动状态

    systemctl status mysqld

    开机启动

    systemctl enable mysqld

    修改root本地登录密码

    grep 'temporary password' /var/log/mysqld.log
    mysql -uroot -p
    set password for 'root'@'localhost'=password('!2Qw32sd');
    mysqladmin -uroot -p123456 password 123

    注意:mysql5.7默认安装了密码安全检查插件(validate_password),默认密码检查策略要求密码必须包含:大小写字母、数字和特殊符号,并且长度不能少于8位。否则会提示ERROR 1819 (HY000): Your password does not satisfy the current policy requirements错误

    配置默认编码为utf8
    修改/etc/my.cnf配置文件,在[mysqld]下添加编码配置,如下所示:

    [mysqld]
    character_set_server=utf8
    init_connect='SET NAMES utf8'

    [远程连接腾讯云MySQL数据库]
    输入mysql -uroot -p,使用Mysql
    use mysql
    update user set host = '%' where user = 'root'

    service mysqld restart

    select host ,user,password from user;
    grant all privileges on . to 'root'@'%' identified by 'root' with grant option;
    flush privileges;

    python manage.py inspectdb
    python manage.py inspectdb > app/models.py

    (四)防火墙配置

    查看firewall服务状态
    systemctl status firewalld
    查看firewall的状态
    firewall-cmd --state

    开启
    service firewalld start
    重启
    service firewalld restart
    关闭
    service firewalld stop
    查看防火墙规则
    firewall-cmd --list-all

    添加
    firewall-cmd --zone=public --add-port=80/tcp --permanent (--permanent永久生效,没有此参数重启后失效)
    firewall-cmd --zone=public --add-port=80/tcp --permanent
    重新载入
    firewall-cmd --reload
    查看
    firewall-cmd --zone= public --query-port=80/tcp
    删除
    firewall-cmd --zone= public --remove-port=80/tcp --permanent

    重启防火墙(修改配置后要重启防火墙)
    firewall-cmd --reload

    参数解释
    1、firwall-cmd:是Linux提供的操作firewall的一个工具;
    2、--permanent:表示设置为持久;
    3、--add-port:标识添加的端口;

    systemctl list-unit-files |grep fire #查开机启动状态

    systemctl enable firewalld.service
    封杀ip
    firewall-cmd --permanent --add-rich-rule="rule family='ipv4' source address='61.177.172.47' reject

    (五)安装配置Nginx和Gunicorn

    在centos7上面安装nginx:
    yum install epel-release
    yum install nginx
    systemctl start nginx.service
    systemctl enable nginx.service
    nginx -s quit

    pip install gunicorn
    nginx -t 查看nginx配置
    gunicorn -c gunicorn.conf.py new.wsgi:application

    vi /etc/nginx/nginx.conf

    server {
    listen 80;
    server_name 193.112.64.245;
    server_name 127.0.0.1;
    server_name www.17mtd.com;

        location / {
        proxy_pass http://127.0.0.1:8000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
        location /static {
        alias /home/new/static;
    }
    

    }

    gunicorn.conf.py

    import multiprocessing
    bind = "127.0.0.1:8000"
    workers = 2
    errorlog = '/home/new/gunicorn.error.log'
    proc_name = 'new'

    将DEBUG值修改为False
    注释以下部分
    STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static')
    添加如下代码
    STATIC_ROOT = os.path.join(BASE_DIR, 'static')

    列出所有端口: netstat -ntlp
    查看端口占用:lsof -i tcp:22
    先用命令查询日志

    cat /var/log/secure | awk '/Failed/{print (NF-3)}' | sort | uniq -c | awk '{print2" = "$1;}'

    如果发现很多数据, 说明很多软件在登录破解你的服务器, 如果没有错误数据, 那倒问题不大

    相关文章

      网友评论

          本文标题:从零开发搭建部署Django+Nginx+Gunicorn网站

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