zoukankan      html  css  js  c++  java
  • Title

    一、购买服务器

      推荐 vultr的服务器,还可以_ _ _,链接:传送门

      操作系统建议选 ubuntu 14.04 64位

    二、购买域名

      链接:传送门

    三、安装相关软件

    # 创建一个叫mu的用户
    root@localhost:~# useradd -m -s /bin/bash mu
    
    # 把新创建的用户加入超级权限组
    root@localhost:~# usermod -a -G sudo mu
    
    # 为新用户设置密码
    # 注意在输密码的时候不会有字符显示,接着敲即可
    root@localhost:~# passwd mu
    
    # 切换到创建的新用户
    root@localhost:~# su - mu
    
    # 切换成功
    mu@localhost:~$

       更新系统

    mu@localhost:~$ sudo apt-get update
    mu@localhost:~$ sudo apt-get upgrade

      安装Nginx、pip、virtualenv

    mu@localhost:~$ sudo apt-get install nginx
    mu@localhost:~$ sudo apt-get install git python3 python3-pip
    mu@localhost:~$ sudo pip3 install virtualenv

       启动Nginx

    mu@localhost:~$ sudo service nginx start

       在项目的配置文件中修改成如下:

    DEBUG = False
    ALLOWED_HOSTS = ['*']
    STATIC_URL = '/static/'
    #STATICFILES_DIRS = (
    #    os.path.join(BASE_DIR,'static'),
    #)
    # 加入下面的配置
    STATIC_ROOT = os.path.join(BASE_DIR, 'static')

       生成安装依赖文件,在manage.py目录下打开cmd运行以下命令

    pip freeze > requirements.txt

      将项目推送到github上

      一台服务器可能部署多个网站,所有网站代码都放在 sites/ 目录下。

    home/mu/
        sites/
            tianbaoo.fun/
                env
                blog-sky
            ackblog/
                env
                ackblog
            awmonline/
                env
                AwmOnlin

       上面是三个网站的文件,各自有各自的env和项目

       举例如何创建tianbaoo.fun以下的两个也是类似的创建方法。

    mu@localhost:~$ mkdir -p ~/sites/tianbaoo.fun
    mu@localhost:~$ cd ~/sites/tianbaoo.fun
    mu@localhost:~/sites/tianbaoo.fun$ virtualenv --python=python3 env
    
    # 拉取项目文件
    mu@localhost:~/sites/tianbaoo.fun$ git clone https://github.com/tianbaoo/blog-sky.git
    
    # 激活虚拟环境并安装依赖
    mu@localhost:~/sites/tianbaoo.fun$ source env/bin/activate
    (env) mu@localhost:~/sites/tianbaoo.fun$ cd blog-sky/
    (env) mu@localhost:~/sites/tianbaoo.fun/blog-sky$ pip install -r requirements.txt
    
    # 收集静态文件
    (env) mu@localhost:~/sites/tianbaoo.fun/blog-sky$ python manage.py collectstatic
    
    # 生成数据库
    (env) mu@localhost:~/sites/tianbaoo.fun/blog-sky$ python manage.py migrate
    
    # 创建超级用户
    (env) mu@localhost:~/sites/tianbaoo.fun/blog-sky$ python manage.py createsuperuser

    四、配置Nginx

      先在服务器的 /etc/nginx/sites-available/ 目录下新建一个配置文件,文件名我一般就设置为域名 

    /etc/nginx/sites-available/tianbaoo.fun
    
    server {
        charset utf-8;
        listen 80;
        server_name tianbaoo.fun; 
    
        location /static { 
            alias /home/mu/sites/tianbaoo.fun/blog-sky/static; 
        }
    
        location / { 
            proxy_set_header Host $host;
            # /tmp/tianbaoo.fun中的 tianbaoo.fun是sites目录下的文件夹名
            proxy_pass http://unix:/tmp/tianbaoo.fun.socket;
        }
    }

       多个网站项目时,在上面的文件里边多写几个server监听不同的端口即可,下面会有介绍。

       建立软连接

    (env) mu@localhost:~/sites/tianbaoo.fun/blog-sky$ sudo ln -s /etc/nginx/sites-available/tianbaoo.fun /etc/nginx/sites-enabled/tianbaoo.fun

       只能看到 Nginx 欢迎页面的问题:

        sites-enabled文件夹里默认的default文件中的配置覆盖了自己写的配置,导致配置不生效,把default文件删掉就可以正常

      

      安装gunicorn

    (env) mu@localhost:~/sites/tianbaoo.fun/blog-sky$ pip install gunicorn

       自动启动 Gunicorn

       写一个启动脚本,这样当服务器重启后能自动引导 Gunicorn 的启动。脚本位于 /etc/init/ 目录下,且脚本文件名必须以 .conf 结尾:

    # /etc/init/gunicorn-tianbaoo.fun.conf
    
    start on net-device-up 
    stop on shutdown
    
    respawn 
    
    setuid mu 
    chdir /home/mu/sites/tianbaoo.fun/blog-sky 
    
    #
    blog-sky.wsgi blog-sky是wsgi.py所在的目录名
    exec ../env/bin/gunicorn --bind unix:/tmp/tianbaoo.fun.socket blog-sky.wsgi:application

       用 start 命令启动 Gunicorn

    (env) mu@localhost:~/sites/tianbaoo.fun/blog-sky$ sudo start gunicorn-tianbaoo.fun
    
    # 以后如果更新了代码,只要运行下面的命令重启一下 Nginx 和 Gunicorn 就可以使新的代码生效了:
    sudo service nginx reload
    sudo restart gunicorn-tianbaoo.fun

    五、一个服务器部署多个网站

      我们现在有三个网站项目文件夹:tianbaoo.fun、ackblog、awmonline,文件夹里对应着它们自己的虚拟环境和实际项目文件

    home/mu/
        sites/
            tianbaoo.fun/
                env
                blog-sky
            ackblog/
                env
                ackblog
            awmonline/
                env
                AwmOnlin

      我们现在已经成功运行了tianbaoo.fun文件夹里的blog-sky项目,现在要来多添加ackblog和AwmOnline项目。

      下面先加ackblog项目

    mu@localhost:~$ mkdir -p ~/sites/ackblog
    mu@localhost:~$ cd ~/sites/ackblog
    mu@localhost:~/sites/ackblog$ virtualenv --python=python3 env
    
    # 拉取项目文件
    mu@localhost:~/sites/ackblog$ git clone https://github.com/tianbaoo/ackblog.git
    
    # 激活虚拟环境并安装依赖
    mu@localhost:~/sites/ackblog$ source env/bin/activate
    (env) mu@localhost:~/sites/ackblog$ cd blog-sky/
    (env) mu@localhost:~/sites/ackblog/ackblog$ pip install -r requirements.txt
    
    # 收集静态文件
    (env) mu@localhost:~/sites/ackblog/ackblog$ python manage.py collectstatic
    
    # 生成数据库
    (env) mu@localhost:~/sites/ackblog/ackblog$ python manage.py migrate
    
    # 创建超级用户
    (env) mu@localhost:~/sites/ackblog/ackblog$ python manage.py createsuperuser

      在 /etc/nginx/sites-available/tianbaoo.fun文件里我们写了第一个项目blog-sky的server

      现在我们同样在里面写入第二个项目ackblog的server信息

    # 这个文件里包含了三个项目的server
    server {
        charset utf-8;
        listen 80;
        server_name 207.246.124.116;
    
        location /static {
        }
    
        location / {
            proxy_set_header Host $host;
            proxy_pass http://unix:/tmp/tianbaoo.fun.socket;
        }
    }
    server {
        charset utf-8;
        listen 6060;
        server_name 207.246.124.116;
    
        location /static {
            alias /home/mu/sites/awmonline/AwmOnline/static;
        }
    
        location / {
            proxy_set_header Host $host;
            proxy_pass http://unix:/tmp/awmonline.socket;
        }
    }
    server {
        charset utf-8;
        listen 5050;
        server_name 207.246.124.116;
    
        location /static {
            alias /home/mu/sites/ackblog/ackblog/static;
        }
    
        location / {
            proxy_set_header Host $host;
            proxy_pass http://unix:/tmp/ackblog.socket;
        }
    }
    View Code

      添加完server之后要在ackblog的虚拟环境中安装gunicorn

    (env) mu@localhost:~/sites/ackblog/ackblog$ pip install gunicorn

      设置ackblog的自启动gunicorn脚本

    # /etc/init/gunicorn-ackblog.conf
    
    start on net-device-up 
    stop on shutdown
    
    respawn 
    
    setuid mu 
    chdir /home/mu/sites/ackblog/ackblog
    
    exec ../env/bin/gunicorn --bind unix:/tmp/ackblog.socket ackblog.wsgi:application 
    
    (env) mu@localhost:~/sites/ackblog/ackblog$ sudo start gunicorn-ackblog

      以后如果更新了代码,只要运行下面的命令重启一下 Nginx 和 Gunicorn 就可以使新的代码生效了:

    sudo service nginx reload
    sudo restart gunicorn-ackblog

      

      第三个项目awmonlin过程步骤和第二个的步骤是一样的,就是有些参数不同

    mu@localhost:~$ mkdir -p ~/sites/awmonline
    mu@localhost:~$ cd ~/sites/awmonline
    mu@localhost:~/sites/awmonline$ virtualenv --python=python3 env
    
    # 拉取项目文件
    mu@localhost:~/sites/awmonline$ git clone https://github.com/tianbaoo/AwmOnline.git
    
    # 激活虚拟环境并安装依赖
    mu@localhost:~/sites/awmonline$ source env/bin/activate
    (env) mu@localhost:~/sites/awmonline$ cd AwmOnline/
    (env) mu@localhost:~/sites/awmonline/AwmOnline$ pip install -r requirements.txt
    
    # 收集静态文件
    (env) mu@localhost:~/sites/awmonline/AwmOnline$ python manage.py collectstatic
    
    # 生成数据库
    (env) mu@localhost:~/sites/awmonline/AwmOnline$ python manage.py migrate
    
    # 创建超级用户
    (env) mu@localhost:~/sites/awmonline/AwmOnline$ python manage.py createsuperuser

      在 /etc/nginx/sites-available/tianbaoo.fun文件里我们写了第一个项目blog-sky的server和第二个项目ackblog的server

      现在我们同样在里面写入第三个项目AwmOnline的server信息

    server {
        charset utf-8;
        listen 80;
        server_name 207.246.124.116;
    
        location /static {
        }
    
        location / {
            proxy_set_header Host $host;
            proxy_pass http://unix:/tmp/tianbaoo.fun.socket;
        }
    }
    server {
        charset utf-8;
        listen 6060;
        server_name 207.246.124.116;
    
        location /static {
            alias /home/mu/sites/awmonline/AwmOnline/static;
        }
    
        location / {
            proxy_set_header Host $host;
            proxy_pass http://unix:/tmp/awmonline.socket;
        }
    }
    server {
        charset utf-8;
        listen 5050;
        server_name 207.246.124.116;
    
        location /static {
            alias /home/mu/sites/ackblog/ackblog/static;
        }
    
        location / {
            proxy_set_header Host $host;
            proxy_pass http://unix:/tmp/ackblog.socket;
        }
    }
    View Code

      添加完server之后要在awmonline的虚拟环境中安装gunicorn

    (env) mu@localhost:~/sites/awmonline/AwmOnline$ pip install gunicorn

      设置awmonline的自启动gunicorn脚本

    # /etc/init/gunicorn-awmonline.conf
    
    start on net-device-up 
    stop on shutdown
    
    respawn 
    
    setuid mu 
    chdir /home/mu/sites/awmonline/AwmOnlin
    
    exec ../env/bin/gunicorn --bind unix:/tmp/awmonline.socket AwmOnlin.wsgi:application 
    
    (env) mu@localhost:~/sites/awmonline/AwmOnlin$ sudo start gunicorn-awmonline

      以后如果更新了代码,只要运行下面的命令重启一下 Nginx 和 Gunicorn 就可以使新的代码生效了:

    sudo service nginx reload
    sudo restart gunicorn-awmonline
  • 相关阅读:
    webpack : 无法加载文件 D: odejs ode_globalwebpack.ps1,因为在此系统上禁止运行脚本。
    TypeError: __WEBPACK_IMPORTED_MODULE_0_react___default.a.createContext is not a function报错处理
    React中import的用法
    antd-react-mobile(踩坑记录)
    [转][C#]BarCodeToHTML
    [转][C#]Environment 类
    [转][easyui]右键菜单
    [转][C#]Web.config 相关配置
    [转]模拟键盘输入
    python3 的 zip
  • 原文地址:https://www.cnblogs.com/guotianbao/p/8981605.html
Copyright © 2011-2022 走看看