阿里云服务器部署Djano+Nginx+uWSGI+conda+Https

参考文章

参使用uWSGI和nginx来设置Django和你的web服务器
python3 + Django + uwsgi + nginx 配置部署笔记
不轻松,服务器部署nginx+uwsgi+djangorestfremework+react
http升级为https全过程(通过nginx安装SSL证书)
Nginx操作 | Nginx配置SSL证书
如何用 uWSGI 托管 Django

我的环境

Ubuntu16.04、conda 4.7.10、python 3.6.7、Django 3.0.3

安装uWSGI

pip install uwsgi
这里如果出现了问题,可以考虑如下解决办法:参考链接

apt-get install python3-dev
apt-get install gcc-4.7 ##卸载你的gcc版本,安装为4.7:
rm /usr/bin/gcc
ln -s /usr/bin/gcc-4.7 /usr/bin/gcc

最后重新运行安装命令pip install uwsgi

测试uWSGI是否正常工作

  • 新建一个test.py文件:touch test.py 若之后要删除则运行rm 文件名
    输入如下内容:
def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return [b"Hello World"] # python3
    #return ["Hello World"] # python2
  • 运行uWSGI:uwsgi --http :8080 --wsgi-file test.py
    可能会报错:
    uwsgi: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory
    解决方法:参考链接
find / -name libpcre.so.* ##找到所有的系统中libpcre
ln -s /root/anaconda3/lib/libpcre.so.1 /lib ##创建libpcre.so.1软链到/lib下
which uwsgi ##测试一下是否好用了

这里有个坑,如果你的服务器是云服务器,例如我的是阿里云,一定要注意设置运行的端口要和云服务器控制台的安全组端口对应,否则可能无法正确地用http访问端口。
安全组配置示例

  • 浏览器键入http://example.com:8080出现Hello World说明运行成功。
    表示以下路径正常:the web client <-> uWSGI <-> Python
    PS.Ubuntu杀死指定进程的命令:kill -9 $(lsof -i tcp:8080 -t)

从简单的test.py到Django项目

  • 新建一个Django项目并确保其能正确运行:如何新建参考这里
    PS.云服务器上记得要把Django项目的settings.py中的ALLOWED_HOSTS = ['']改为ALLOWED_HOSTS = ['*'],方便其他地址正常访问。
  • 改用uWSGI来运行:uwsgi --http :8080 --module mysite.wsgi
    如果能正常运行说明以下路径正常:the web client <-> uWSGI <-> Django
    通常我们不会让浏览器直接与uWSGI通信,那是web服务器的工作。

安装Nginx

sudo apt-get install nginx
sudo /etc/init.d/nginx start ##启动nginx服务

通过浏览器访问80端口,你应该会从Nginx获得一个消息:”Welcome to nginx!”。
这说明以下路径正常:the web client <-> the web server

配置Nginx静态路径

  • 在Django目录下新建名为uwsgi_params的文件,文件内容从这里复制
  • 在Django目录下新建名为mysite_nginx.conf的文件,内容为:
# mysite_nginx.conf

# the upstream component nginx needs to connect to
upstream django {
    #server 0.0.0.0:8081; # for a web port socket (we'll use this first)
}

# configuration of the server
server {
    # the port your site will be served on
    listen      8080;
    # the domain name it will serve for
    server_name xx.xx.xx.xx # substitute your machine's IP address or FQDN
    charset     utf-8;

    # max upload size
    client_max_body_size 75M;   # adjust to taste

    # Django media
    location /media  {
        alias /home/mysite/media;  # your Django project's media files - amend as required
    }

    location /static {
        alias /home/mysite/static; # your Django project's static files - amend as required
    }

    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django;
        include     /home/mysite/uwsgi_params; # the uwsgi_params file you installed,示使用uwsgi代理
    }
}
  • 将上述文件链接到/etc/nginx/sites-enabled中以便Nginx识别
    sudo ln -s ~/path/to/your/mysite/mysite_nginx.conf /etc/nginx/sites-enabled/
  • 修改Django的settings.py文件,添加下述语句:
    STATIC_ROOT = os.path.join(BASE_DIR, "static/")
  • 运行如下命令:
    python manage.py collectstatic这样Django就会收集静态文件,放到指定目录(static目录)内
  • 重启Nginx
    sudo /etc/init.d/nginx restart
  • 测试
    将测试图片test.jpg放入media文件夹中,访问xx.xx.xx.xx:8080/media/test.jpg如果能访问,说明nginx提供了正确的文件服务

配置Nginx动态请求

让Nginx对test.py应用说句”hello world”吧。
uwsgi --socket :8081 --wsgi-file test.py

  • socket :8081:使用uWSGI协议,端口为8081,同时,已经配置了Nginx在那个端口与uWSGI通信,而对外使用8000端口。访问:xx.xx.xx.xx:8080/
    出现“hello world”说明以下路径正常:the web client <-> the web server <-> the socket <-> uWSGI <-> Python

使用Unix socket而不是端口

目前我们使用了简单的TCP socket,换成Unix socket所用的开销更小。

  • 编辑mysite_nginx.conf文件
    将第一句的server 0.0.0.0:8081; #web socket改为server unix:///path/to/your/mysite/mysite.sock; 这里的/path/to/your/.....改成你自己的路径,mysite.sock是系统自动生成的,不用理会它。
  • 重启Nginx:sudo /etc/init.d/nginx restart
  • 再次运行uWSGI:uwsgi --socket mysite.sock --wsgi-file test.py
  • 在浏览器访问:xx.xx.xx.xx:8000/
    这里可能会出现502 Bad Gateway,原因是Nginx没有进入该目录的权限,故无法访问socket文件,其中一个解决方法是改变运行Nginx的用户身份,把/etc/nginx/nginx.conf 第一行的user www-data; 中的www-data 改成权限足够高的用户,重启Nginx。我直接改成root了,可以正常运行。

使用uWSGI和Nginx运行Django应用

  • 输入以下命令运行Django应用
    uwsgi --socket mysite.sock --module mysite.wsgi --chmod-socket=664

通过配置文件启动uWSGI

到这一步时,如果关闭云服务器的ssh远程连接,网站又会出现502 Bad Gateway了。这是因为当你关闭这个ssh进程时,uWSGI进程也被终止了,所以我们需要ini配置文件启动。

  • 在项目目录下新建mysite_uwsgi.ini文件,内容如下:
# mysite_uwsgi.ini file
[uwsgi]

# Django-related settings
# the base directory (full path)
chdir           = /home/mysite
# Django's wsgi file
module          = mysite.wsgi
# the virtualenv (full path)
virtualenv      = /root/anaconda3/envs/chineseocr
python-autoreload=1
# process-related settings
# master
master          = true
# maximum number of worker processes
processes       = 10
# the socket (use the full path to be safe
socket          = /home/mysite/mysite.sock
# ... with appropriate permissions - may be needed
# chmod-socket    = 664
# clear environment on exit
vacuum          = true

  • 然后使用这个文件运行uWSGI,再次测试Django项目是否运行正常
    uwsgi --ini mysite_uwsgi.ini # the --ini option is used to specify a file

Nginx静态资源位置

访问网址80端口默认指向/usr/share/nginx/html目录下的index.html
mysite_nginx.conf文件中,我们在server{}下添加root /home/mysite/wwwroot/build;可以改变主页的默认位置。

Nginx添加SSL证书

PS.这一节配置在我机器上有点问题,大家可以参考下其他人的文章。我用了下一节在其他端口配置的SSL可以正常使用,如果你只需要某些端口的使用,可以直接参考下一节。

  • 首先在阿里云申请证书:http升级为https全过程(通过nginx安装SSL证书)
  • 检查Nginx有没有安装SSL模块:
    进入Nginx安装目录,我的是/etc/,输入nginx -V 查看已经安装的模块,SSL的模块名是--with-http_ssl_module
  • 在/etc/nginx/conf.d目录里添加一个文件,文件名以.conf结尾,输入下面两个server:
server {
	listen 443 ssl;   #SSL协议访问端口号为443。此处如未添加ssl,可能会造成Nginx无法启动。
	server_name localhost;  #将localhost修改为您证书绑定的域名,例如:www.example.com。
	root html;
	index index.html index.htm;
	ssl_certificate cert/domain name.pem;   #将domain name.pem替换成您证书的文件名。
	ssl_certificate_key cert/domain name.key;   #将domain name.key替换成您证书的密钥文件名。
	ssl_session_timeout 5m;
	ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;  #使用此加密套件。
	ssl_protocols TLSv1 TLSv1.1 TLSv1.2;   #使用该协议进行配置。
	ssl_prefer_server_ciphers on;   
	location / {
		root html;   #站点目录。
		index index.html index.htm;   
	}
} 
server {
	listen 80;
	server_name localhost;   #将localhost修改为您证书绑定的域名,例如:www.example.com。
	rewrite ^(.*)$ https://$host$1 permanent;   #将所有http请求通过rewrite重定向到https。
	location / {
		index index.html index.htm;
	}
}
  • 重新启动服务器
    sudo /etc/init.d/nginx restart
  • 一些辅助命令
    检查.conf文件有无语法错误:nginx -t -c /etc/nginx/nginx.conf
    查看服务器无法启动的原因:systemctl status nginx.service
  • 注意要在云服务器安全组(防火墙)开放HTTPS的443端口,不然无法访问

给其他端口开启SSL服务

  • 在/etc/nginx/site-enabled下新建test.conf文件
    假设该端口可以处理静态资源请求,动态请求转接给Django,文件内容如下:
server {
    listen      8083 ssl;
    server_name www.xxx.com;
    charset     utf-8;
    ssl_certificate /etc/nginx/cert/xxx.pem;
    ssl_certificate_key /etc/nginx/cert/xxx.key;
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;  #阿里云证书,使用此加密套件。
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;   #使用该协议进行配置。
    ssl_prefer_server_ciphers on;

    # max upload size
    client_max_body_size 75M;   # adjust to taste

    # Django media
    location /media  {
        alias /home/mysite/media;  # your Django project's media files - amend as required
    }

    location /static {
        alias /home/mysite/static; # your Django project's static files - amend as required
    }

    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django;
        include     /home/mysite/uwsgi_params; # the uwsgi_params file you installed
    }
}
  • 输入https://www.xxx.com:8083即可正常访问
原文地址:https://www.cnblogs.com/wuu02/p/12403956.html