CentOS环境安装Docker配置nginx+uwsgi+django

Docker安装

1、[root@CentOS ~]# yum install docker
2、[root@CentOS ~]# service docker start
3、[root@CentOS ~]# chkconfig docker on

Docker安装Nginx

[root@CentOS ~]# docker pull nginx

Docker安装MySQL

[root@CentOS ~]# docker pull mysql

Docker安装Python

[root@CentOS ~]# docker pull python

Docker安装Redis

[root@CentOS ~]# docker pull redis

Docker安装MongoDB

[root@CentOS ~]# docker pull mongo

一、Docker环境安装CentOS
1、查找镜像源

[root@CentOS ~]# docker search centos
INDEX       NAME                                         DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
docker.io   docker.io/centos                             The official build of CentOS.                   5070      [OK]       
docker.io   docker.io/ansible/centos7-ansible            Ansible on Centos7                              119                  [OK]
docker.io   docker.io/jdeathe/centos-ssh                 CentOS-6 6.10 x86_64 / CentOS-7 7.5.1804 x...   104                  [OK]
docker.io   docker.io/consol/centos-xfce-vnc             Centos container with "headless" VNC sessi...   73                   [OK]
docker.io   docker.io/imagine10255/centos6-lnmp-php56    centos6-lnmp-php56                              48                   [OK]
docker.io   docker.io/centos/mysql-57-centos7            MySQL 5.7 SQL database server                   45                   
docker.io   docker.io/tutum/centos                       Simple CentOS docker image with SSH access      43                   
docker.io   docker.io/gluster/gluster-centos             Official GlusterFS Image [ CentOS-7 +  Glu...   38                   [OK]
docker.io   docker.io/openshift/base-centos7             A Centos7 derived base image for Source-To...   37                   
docker.io   docker.io/centos/postgresql-96-centos7       PostgreSQL is an advanced Object-Relationa...   36                   
docker.io   docker.io/centos/python-35-centos7           Platform for building and running Python 3...   33                   
docker.io   docker.io/kinogmt/centos-ssh                 CentOS with SSH                                 25                   [OK]
docker.io   docker.io/openshift/jenkins-2-centos7        A Centos7 based Jenkins v2.x image for use...   20                   
docker.io   docker.io/centos/php-56-centos7              Platform for building and running PHP 5.6 ...   17                   
docker.io   docker.io/pivotaldata/centos-gpdb-dev        CentOS image for GPDB development. Tag nam...   10                   
docker.io   docker.io/openshift/wildfly-101-centos7      A Centos7 based WildFly v10.1 image for us...   6                    
docker.io   docker.io/openshift/jenkins-1-centos7        DEPRECATED: A Centos7 based Jenkins v1.x i...   4                    
docker.io   docker.io/darksheer/centos                   Base Centos Image -- Updated hourly             3                    [OK]
docker.io   docker.io/pivotaldata/centos                 Base centos, freshened up a little with a ...   2                    
docker.io   docker.io/pivotaldata/centos-mingw           Using the mingw toolchain to cross-compile...   2                    
docker.io   docker.io/blacklabelops/centos               CentOS Base Image! Built and Updates Daily!     1                    [OK]
docker.io   docker.io/pivotaldata/centos-gcc-toolchain   CentOS with a toolchain, but unaffiliated ...   1                    
docker.io   docker.io/pivotaldata/centos7-build          CentosOS 7 image for GPDB compilation           0                    
docker.io   docker.io/pivotaldata/centos7-test           CentosOS 7 image for GPDB testing               0                    
docker.io   docker.io/smartentry/centos                  centos with smartentry                          0                    [OK]

2、下载镜像

[root@CentOS ~]# docker pull docker.io/centos

3、查看已下载的镜像

[root@CentOS ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/centos    latest              1e1148e4cc2c        2 weeks ago         202 MB
docker.io/nginx     latest              568c4670fa80        3 weeks ago         109 MB
docker.io/mongo     latest              525bd2016729        5 weeks ago         383 MB
docker.io/tomcat    latest              78b258e36eed        5 weeks ago         463 MB
docker.io/python    latest              1e80caffd59e        5 weeks ago         923 MB
docker.io/redis     latest              c188f257942c        5 weeks ago         94.9 MB
docker.io/mysql     latest              f991c20cb508        5 weeks ago         486 MB
docker.io/httpd     latest              2a51bb06dc8b        5 weeks ago         132 MB
docker.io/jenkins   latest              cd14cecfdb3a        5 months ago        696 MB

[root@CentOS ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/centos    latest              1e1148e4cc2c        2 weeks ago         202 MB
docker.io/nginx     latest              568c4670fa80        3 weeks ago         109 MB
docker.io/mongo     latest              525bd2016729        5 weeks ago         383 MB
docker.io/tomcat    latest              78b258e36eed        5 weeks ago         463 MB
docker.io/python    latest              1e80caffd59e        5 weeks ago         923 MB
docker.io/redis     latest              c188f257942c        5 weeks ago         94.9 MB
docker.io/mysql     latest              f991c20cb508        5 weeks ago         486 MB
docker.io/httpd     latest              2a51bb06dc8b        5 weeks ago         132 MB
docker.io/jenkins   latest              cd14cecfdb3a        5 months ago        696 MB                                                                        

4、启动镜像

[root@CentOS ~]# docker run -itd centos /bin/bash
WARNING: IPv4 forwarding is disabled. Networking will not work.
37c8eec506e8cb6867c2352dee59fc47649e903a469ab07bc561c8e3407dcd0a
问题:创建容器的时候报错WARNING: IPv4 forwarding is disabled. Networking will not work. 
解决办法:
打开文件:[root@CentOS ~]# vi /usr/lib/sysctl.d/00-system.conf
添加如下代码:net.ipv4.ip_forward=1
重启network服务:[root@CentOS ~]# systemctl restart network
完成以后,删除错误的容器,再次创建新容器

删除容器
查看所有运行或者不运行容器
[root@CentOS ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
37c8eec506e8        centos              "/bin/bash"         7 minutes ago       Up 7 minutes                            laughing_engelbart

停止运行容器
[root@CentOS ~]# docker stop 37c8eec506e8

删除容器
[root@CentOS ~]# docker rm 37c8eec506e8

重新创建容器
[root@CentOS ~]#  docker run -itd -p 80:9000 --name centos docker.io/centos  /bin/bash

5、查看容器

[root@CentOS ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                  NAMES
050e1835cbc8        docker.io/centos    "/bin/bash"         51 seconds ago      Up 50 seconds       0.0.0.0:80->9000/tcp   centos

6、进入容器

[root@CentOS ~]# docker exec -it 050e1835cbc8 /bin/bash
[root@CentOS ~]# docker attach 050e1835cbc8
[root@050e1835cbc8 /]# 

7、通过配置文件修改docker容器端口映射

[root@CentOS ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                NAMES
d916e60cd091        docker.io/centos    "/bin/bash"         2 days ago          Up 6 minutes        0.0.0.0:80->80/tcp   centos

[root@CentOS ~]# docker inspect d916e60cd091|grep Id
        "Id": "d916e60cd09151085a9dc08377cb3e152b5dc86197b8591bbf5b0a129c840760",

[root@CentOS ~]# docker stop centos

[root@CentOS ~]# cd /var/lib/docker/containers/

停止 docker 引擎服务,systemctl stop docker 或者 service docker stop

进入对应 Id 所在目录后,修改 /var/lib/docker/containers/{ 容器 ID+ 一些字符串}/hostconfig.json 中
"PortBindings":{"容器端口 /tcp":[{"HostIp":"","HostPort":"宿主机端口"}]
例如:
"PortBindings":{"80/tcp":[{"HostIp":"","HostPort":"80"}],"8080/tcp":[{"HostIp":"","HostPort":"8080"}],"8888/tcp":[{"HostIp":"","HostPort":"8888"}]}

同时修改同目录下的 config.v2.json 中 
"ExposedPorts":{"容器端口 /tcp":{}}
"Ports":{"容器端口 /tcp":[{"HostIp":"0.0.0.0","HostPort":"宿主机端口"}]} 
例如:
"ExposedPorts":{"80/tcp":{},"8080/tcp":{},"8888/tcp":{}}
"Ports":{"80/tcp":[{"HostIp":"0.0.0.0","HostPort":"80"}],"8080/tcp":[{"HostIp":"0.0.0.0","HostPort":"8080"}],"8888/tcp":[{"HostIp":"0.0.0.0","HostPort":"8888"}]}

重启服务, 启动容器 (systemctl start docker/service docker start)

8、查看docker容器IP

想看到docker容器的ip先安装net-tools
[root@050e1835cbc8 ~]# yum install net-tools -y
之后就可以用ifconfig查看
[root@050e1835cbc8 ~]# ifconfig

二、CentOS环境安装Python3.7

1、安装编译相关工具

[root@050e1835cbc8 /]# yum -y groupinstall "Development tools"
[root@050e1835cbc8 /]# yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel
[root@050e1835cbc8 /]# yum install libffi-devel -y

2、下载安装包解压

[root@050e1835cbc8 ~]# yum -y install wget
[root@050e1835cbc8 ~]# wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tar.xz
[root@050e1835cbc8 ~]# tar -xvJf  Python-3.7.0.tar.xz

3、编译安装

[root@050e1835cbc8 ~]# mkdir /usr/local/python3 #创建编译安装目录
[root@050e1835cbc8 ~]# cd Python-3.7.0
[root@050e1835cbc8 Python-3.7.0]# ./configure --prefix=/usr/local/python3
[root@050e1835cbc8 Python-3.7.0]# make && make install

4、创建软连接

[root@050e1835cbc8 ~]# ln -s /usr/local/python3/bin/python3 /usr/local/bin/python3
[root@050e1835cbc8 ~]# ln -s /usr/local/python3/bin/pip3 /usr/local/bin/pip3

5、验证是否成功

[root@050e1835cbc8 ~]# python3 -V
[root@050e1835cbc8 ~]# pip3 -V

三、CentOS环境安装Nginx

1、gcc 安装
安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境。nginx也需要 gcc 环境。

[root@050e1835cbc8 ~]# yum install gcc-c++

2、pcre pcre-devel 安装
PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也需要此库。

[root@050e1835cbc8 ~]# yum install -y pcre pcre-devel

3、zlib 安装
zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos 上安装 zlib 库。

[root@050e1835cbc8 ~]# yum install -y zlib zlib-devel

4、openssl openssl-devel安装
OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库。

[root@050e1835cbc8 ~]# yum install -y openssl openssl-devel

5、下载安装包解压

[root@050e1835cbc8 ~]# wget https://nginx.org/download/nginx-1.14.2.tar.gz
[root@050e1835cbc8 ~]# tar -zxvf nginx-1.14.2.tar.gz

6、编译安装

[root@050e1835cbc8 ~]# cd nginx-1.14.2
[root@050e1835cbc8 nginx-1.14.2]# ./configure
*
*
Configuration summary
  + using system PCRE library
  + OpenSSL library is not used
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

[root@050e1835cbc8 nginx-1.14.2]# make
[root@050e1835cbc8 nginx-1.14.2]# make install

7、查找安装路径

[root@050e1835cbc8 nginx-1.14.2]# whereis nginx

8、启动、停止nginx

[root@050e1835cbc8 nginx-1.14.2]# cd /usr/local/nginx/sbin/
[root@050e1835cbc8 sbin]# ./nginx
[root@050e1835cbc8 sbin]# ./nginx -s stop
[root@050e1835cbc8 sbin]# ./nginx -s quit
[root@050e1835cbc8 sbin]# ./nginx -s reload

9、查看nginx是否启动成功

[root@050e1835cbc8 sbin]# curl localhost
[root@050e1835cbc8 sbin]# ps -aux|grep nginx

10、修改nginx配置

[root@050e1835cbc8 sbin]# vi /usr/local/nginx/conf/nginx.conf

*
 server {
        listen       80;#修改端口9000
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }
*

systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall开机启动
firewall-cmd –state #查看默认防火墙状态(关闭后显示notrunning,开启后显示running)

四、CentOS环境通过Python3安装django和uwsgi以及配置

[root@050e1835cbc8 ~]# pip3 install django
*
*
You are using pip version 10.0.1, however version 18.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
安装提示时需要更新pip
[root@050e1835cbc8 ~]# python3 -m pip install --upgrade pip
[root@050e1835cbc8 ~]# pip3 install uwsgi
创建软链接可以在终端使用uwsgi命令
[root@050e1835cbc8 ~]# ln -s /usr/local/python3/bin/uwsgi /usr/bin/uwsgi

进入/home/文件路径
[root@050e1835cbc8 home]# pwd
/home/

创建django项目
[root@CentOS www]# python3 /usr/local/python3/bin/django-admin.py startproject ChinaDaas
进入项目路径
[root@CentOS www]# cd ChinaDaas/
启动项目测试django
[root@CentOS ChinaDaas]# python3 manage.py runserver
    修改settings.py文件ALLOWED_HOSTS = ['*']


uwsgi --ini uwsgi.ini   # 启动uwsgi配置
ps -ef |grep uwsgi 
uwsgi --stop uwsgi.pid  # 关闭uwsgi
pkill -f uwsgi -9 	# 关闭uwsgi
uwsgi --reload uwsgi.pid  #重新加载配置

uwsgi.ini文件

[uwsgi]
    #请求方式与端口号
    socket = :8080
    #日志文件
    daemonize = /usr/local/nginx/uwsgi_temp/uwsgi.log
    pidfile = /usr/local/nginx/uwsgi_temp/uwsgi.pid
    #允许主进程存在
    master = true
    #开启进程数
    processes = 4
    #开启线程数
    threads =2
    #当服务器退出时自动清理环境
    vacuum = true

~
~

nginx.conf文件

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;	
    sendfile        on;	
    keepalive_timeout  65;	
    server {
        listen       8080;
        server_name  localhost  192.168.100.100;
        charset utf-8;
        location / {
            include /usr/local/nginx/conf/uwsgi_params;
            uwsgi_pass  127.0.0.1:8000;
            uwsgi_param UWSGI_SCRIPT ChinaDaas.wsgi;
            uwsgi_param UWSGI_CHDIR /home/ChinaDaas;
        }
		location /templates/ {
			alias /home/ChinaDaas/templates/;
		}
		}
}
原文地址:https://www.cnblogs.com/mrrr/p/10183543.html