django网站部署到服务器

Django
网站建设
Author

stack

Published

March 14, 2024

如果是腾讯云/阿里云服务器需配置网络安全组

HTTP 显示为 80/80。 实例作为网站或 Web 应用的服务器。

    sudo apt install python2
    sudo apt install curl
    curl https://bootstrap.pypa.io/pip/2.7/get-pip.py --output get-pip.py
    sudo python2 get-pip.py
    sudo apt-get install nginx
    sudo service nginx start
    sudo pip install fabric3==1.14.post1   ##必须使用py2对应的pip安装。py2只能用低版本Fabric
    sudo pip install virtualenv
    此时输入网站域名或者IP就能看到welcome to nginx!测试页面
    在包含fabfile.py文件的目录下,运行

    fab deploy
    
    输入域名和linux密码

```bash sudo service nginx reload #在deploy_tools文件夹下保存了默认配置,只需稍作修改将内容复制到系统相应目录下即可

sed "s/SITENAME/ephys.cn/g" deploy_tools/nginx.template.conf | sed "s/USER/ubuntu/g" | sudo tee /etc/nginx/sites-available/ephys.cn

#fabric中进行文本替换的函数叫sed,先把SITENAME替换成网站地址,再把USER替换成当前系统用户名,然后使用管道操作(|)把文本留传给拥有root权限用户写入系统/etc/nginx/sites-available/ephys.cn文件里。

#激活这个配置文件的虚拟主机:

sudo ln -s /etc/nginx/sites-available/ephys.cn       /etc/nginx/sites-enabled/ephys.cn

#然后编写Upstart脚本:

sed "s/SITENAME/ephys.cn/g" deploy_tools/gunicorn-upstart.template.conf | sed "s/USER/ubuntu/g" | sudo tee /etc/init/gunicorn-ephys.cn.conf

#最后启动这两个服务:

sudo service nginx reload
sudo start gunicorn-ephys.cn

- 第五步 为了能获取访问者的ip,特地在nginx.template.conf 加入了以下一段
```bash
    location / {
            proxy_set_header Host $host;
            proxy_pass http://unix:/tmp/ephys.cn.socket;
            
            #IP_adress
            proxy_redirect off;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Scheme $scheme;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        }
server {
    listen 80;
    server_name ephys.cn;

    location /static {
        alias /home/ubuntu/sites/ephys.cn/source/static;
    }
    location /media {
        autoindex on;
        alias /home/ubuntu/sites/ephys.cn/source/media;
    }

    location /teach {
#            root  /home/ubuntu/ephysics/_site/;
            alias /home/ubuntu/ephysics/_site/;

}
    location / {
            proxy_set_header Host $host;
            proxy_pass http://unix:/tmp/ephys.cn.socket;

            proxy_redirect off;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Scheme $scheme;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        }
}

同时注意在ubuntu16.04中upstart无法使用,改成了systemd service

因此在第三步有关upstart部分改为:

    sed "s/SITENAME/ephys.cn/g" deploy_tools/sysd.service 
    | sed "s/USER/ubuntu/g" | sudo tee  /etc/systemd/system/ephys.cn.service
  • 最后 最后启动服务
    sudo service nginx reload
    sudo systemctl daemon-reload
    sudo systemctl enable ephys.cn.service
    sudo systemctl start ephys.cn.service

注意事项

pip安装requests要<2.27

pip install requests==2.23.0   'certifi<=2020.4.5.1'

要加上 ‘certifi<=2020.4.5.1’

安装 requests>2.27 会出现语法问题,与py2.7不兼容。 是时候改成Python3了

Back to top