一、构建docker容器

1、拉取镜像python构建容器

sudo docker pull python

2、构建容器并挂载数据卷

sudo docker run -it --name python3 -v  /home/ubuntu/typeidea:/home/typeidea -p 8000:8000 python:3.6 /bin/bash

/home/ubuntu/typeidea为服务器本地放项目的文件夹;/home/typeidea容器中的文件存放地址;8000:8000将容器的地址端口映射到本地地址端口

二、安装nginx和uwsgi

下载nginx,这里直接在服务器的终端下载就好

apt-get install nginx

查看是否安装成功

nginx -v

出现版本号证明安装成功,通过浏览器访问:106.52.188.166(换成自己服务器的地址),看到下面的界面就成功了
在这里插入图片描述
接下来我们来安装uwsgi
启动我们的python3容器,如果启动就不用了

sudo docker exec -it python3 /bin/bash

安装uwsgi

pip install uwsgi

三、配置nginx和uwsgi

我们先来配置uwsgi
在项目的根目录下创建uwsgi.ini,在uwsgi配置如下信息

[uwsgi]
socket = 0.0.0.0:8000
chdir           = /home/typeidea
module          = typeidea.wsgi:application
master          = true
processes       = 4   
vacuum          = true
buffer-size=65535
~                     

typeidea是我的django项目名
socket = 0.0.0.0:8000不要改动,不然后面你配置nginx的时候也要改端口
chdir = /home/typeidea 放在python3容器中的项目
module = typeidea.wsgi:application 项目中wsgi.py中的文件信息,可以看看你的,其它的一样就行。
这里你可以在开一个界面连接服务器,查看uwsgi在容器中的地址,
在这里插入图片描述
这里记录了你容器的ip地址是:172.17.0.3,对应下面的 uwsgi_pass地址还有端口8000

sudo docker inspect python3

接下来配置nginx
备份/etc/nginx/sites-available文件夹内的default文件,然后编辑它(不同的Nginx版本可能配置方法不一样):(这里可以用vim编辑),可会看到如下信息

# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# Default server configuration
#
server {
    listen 80;
    listen [::]:80;

    # SSL configuration
    #
    # listen 443 ssl default_server;
    # listen [::]:443 ssl default_server;
    #
    # Note: You should disable gzip for SSL traffic.
    # See: https://bugs.debian.org/773332
    #
    # Read up on ssl_ciphers to ensure a secure configuration.
    # See: https://bugs.debian.org/765782
    #
    # Self signed certs generated by the ssl-cert package
    # Don't use them in a production server!
    #
    # include snippets/snakeoil.conf;

    # root /var/www/html;

    # Add index.php to the list if you are using PHP
    # index index.html index.htm index.nginx-debian.html;

    server_name 106.52.188.166;  # 你的服务器ip地址,

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        # try_files $uri $uri/ =404;
        include  uwsgi_params;
                uwsgi_pass  172.17.0.3:8000;  #这里是uwsgi的地址和端口,注意要一样
    }
    location /static {


    }
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #   include snippets/fastcgi-php.conf;
    #
    #   # With php7.0-cgi alone:
    #   fastcgi_pass 127.0.0.1:9000;
    #   # With php7.0-fpm:
    #   fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #   deny all;
    #}
}


# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
#   listen 80;
#   listen [::]:80;
#
#   server_name example.com;
#
#   root /var/www/example.com;
#   index index.html;
#
#   location / {
#       try_files $uri $uri/ =404;
#   }
#}

请将server_name改成你的实际IP。include uwsgi_params一定要一样。uwsgi_pass和你uWSGI配置中的socket要一样。location /static的alias改成你的实际情况,让静态文件得以部署。

修改完毕,保存退出(按ESC键退出编辑,然后再按 **:qw!**键强制退出保存),然后重启nginx服务:

sudo service nginx restart

四、启动服务

下面我们可以尝试启动服务了! 进入容器python3中项目的根目录,也就是有uwsgi.ini文件的地方,运行:

sudo uwsgi uwsgi.ini

再访问浏览器106.52.188.166,就可以看见成功了

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐