美文网首页
我为何又开始用Django了(内含安装和配置超详细文档)

我为何又开始用Django了(内含安装和配置超详细文档)

作者: iCloudEnd | 来源:发表于2019-02-25 16:41 被阅读66次

    写在最前面

    写了几篇文章后,竟然有个简书好友希望帮他做个体能测试成绩的程序。

    由于是多年的朋友了,虽然有报酬但是实在不太想做,俺的《学Swift挣美元》专栏 专栏才刚刚起步,精力实在有限。在朋友软磨硬泡下,还是答应了。本次项目打算用Django做,下面我之前总结的服务器从零开始配置服务器。

    1、替换apt国内更新源
    (1)替换为阿里云的源
    sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
    sudo vim /etc/apt/sources.list


    deb https://mirrors.ustc.edu.cn/ubuntu/ bionic main restricted universe multiverse
    deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic main restricted universe multiverse
    deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
    deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
    deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
    deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
    deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
    deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
    deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
    deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse



    (2)、update一下
    sudo apt-get update

    3、安装pip

    sudo apt-get install python3-pip

    4、安装nginx
    sudo apt-get install nginx

    5、virtualenv和virtualenvwrapper
    5.1安装virtualenv和virtualenvwrapper
    pip install -i https://pypi.douban.com/simple virtualenv
    pip install -i https://pypi.douban.com/simple virtualenvwrapper

    如果提示权限不足,请用sudo
    5.2配制workon
    5.2.1创建虚拟环境管理目录
    mkdir ~/.virtualenvs
    5.2.2配制bashrc
    5.2.2.1看看路径
    sudo find / -name "virtualenv"
    /home/guanli/.local/bin/virtualenv
    sudo find / -name "virtualenvwrapper.sh"
    /home/guanli/.local/bin/virtualenvwrapper.sh
    5.2.2.3添加下面两行到bashrc
    sudo vim ~/.bashrc

    export WORKON_HOME=$HOME/.virtualenvs
    source /home/guanli/.local/bin/virtualenvwrapper.sh

    5.2.2.4执行修改并添加软链接
    source ~/.bashrc
    sudo ln -s /home/guanli/.local/bin/virtualenv /usr/bin/virtualenv

    5.2.3 创建虚拟环境
    mkvirtualenv mxonline

    5.2.4 安装django
    pip install -i https://pypi.douban.com/simple django

    6、安装uwsgi
    deactivate 返回guanli账号

    sudo apt-get install gcc
    sudo apt-get install python3.6-dev
    pip install -i https://pypi.douban.com/simple setuptools
    pip install -i https://pypi.douban.com/simple uwsgi
    sudo pip install -i https://pypi.douban.com/simple uwsgi
    7、上传django项目

    8、配置nginx
    8.1链接配置文件
    sudo ln -s /home/guanli/mysite/conf/nginx/uc_nginx.conf /etc/nginx/conf.d/
    8.2修改nginx.conf
    sudo vim /etc/nginx/nginx.conf
    ubuntu 请在nginx.conf 中注释掉

    include /etc/nginx/sites-enabled/*;

    9.启动服务

    sudo mkdir -p /etc/uwsgi/sites
    sudo ln -s /home/guanli/mysite/conf/uwsgi.ini /etc/uwsgi/sites/
    sudo vim /etc/systemd/system/uwsgi.service
    sudo ln -s /home/guanli/mysite/conf/uwsgi.service /etc/systemd/system/
    sudo systemctl enable nginx
    sudo systemctl enable uwsgi

    ---------=

    9、制作启动脚本
    sudo chmod a+x /home/guanli/mysite/myshell/start.sh
    测试
    sh /home/guanli/mysite/myshell/start.sh
    添加到rc.local

    !/bin/sh -e

    rc.local

    This script is executed at the end of each multiuser runlevel.

    Make sure that the script will "exit 0" on success or any other

    value on error.

    In order to enable or disable this script just change the execution

    bits.

    By default this script does nothing.

    echo "this is a test" > /usr/local/text.log
    sh /home/guanli/mysite/myshell/start.sh
    exit 0

    做完这一步,还需要最后一步 前面我们说 systemd 默认读取 /etc/systemd/system 下的配置文件, 所以还需要在 /etc/systemd/system 目录下创建软链接

    ln -s /lib/systemd/system/rc.local.service /etc/systemd/system/

    最后
    sudo chmod a+x /etc/rc.local
    sudo chmod a+x sh /home/guanli/mysite/myshell/start.sh


    ==========
    mysql

    sudo apt-get install mariadb-server

    sudo more /etc/mysql/debian.cnf
    看到账号和密码

    update mysql.user set authentication_string=password('dbnic123') where user='root' and Host ='localhost';
    update user set plugin="mysql_native_password";
    flush privileges;
    quit;

    UPDATE user SET Password=PASSWORD('dbnic123') where USER='root';

    配置谁都可以链接
    GRANT ALL PRIVILEGES ON . TO 'root'@'localhost' IDENTIFIED BY 'dbnic123' WITH GRANT OPTION;
    FLUSH PRIVILEGES;

    GRANT ALL PRIVILEGES ON . TO 'root'@'%' IDENTIFIED BY 'dbnic123'
    quit;

    安装client
    sudo apt-get install libmysqlclient-dev
    pip install -i https://pypi.douban.com/simple django
    pip install -i https://pypi.douban.com/simple mysqlclient

    mysqladmin -uroot -p password 'dbnic123'

    DATABASES = {
    'default': {
    'ENGINE': 'django.db.backends.sqlite3',
    'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    },
    'mysqldb': {
    'ENGINE': 'django.db.backends.mysql',
    'NAME':'testmysql',
    'USER': 'root',
    'PASSWORD': 'dbnic123',
    'HOST': '127.0.0.1',
    'PORT': '3306',

    }
    

    }

    vim /etc/mysql/mariadb.conf.d/50-server.cnf
    添加'#'注释掉其中的"bind-address = 127.0.0.1",如下图:


    =====

    相关文章

      网友评论

          本文标题:我为何又开始用Django了(内含安装和配置超详细文档)

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