docker下安装nginx和php

    xiaoxiao2022-07-06  205

    docker下安装nginx和php

    安装docker

    我使用的是centos7.0系统,具体安装要求,大家可以去官网上查询

    sudo yum install -y yum-utils device-mapper-persistent-data lvm2 #安装工具,我安装docker的时候并没有安装,也没出现错误,不知道是不是环境的问题 sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo #添加源信息 sudo yum makecache fast # 更新yum缓存 sudo yum -y install docker-ce #安装ce版本 sudo systemctl start docker #启动docker

    安装nginx

    docker pull nginx #下载nginx docker images nginx #查看安装结果 docker run --name nginx-test -p 8081:80 -d nginx #运行一个nginx容器,nginx-test是容器名称

    上面的安装方式是直接安装,代码和配置文件都在容器中,修改不方便,可以用下面的方式运行容器

    mkdir -p /data/www/html /data/nginx/conf /data/nginx/logs #创建个人目录,保存文件信息 docker cp 6dd4380ba708:/etc/nginx/nginx.conf /data/nginx/conf #从一个成功运行的nginx容器中把配置文件复制过来 docker run -d -p 80:80 --name nginx-web -v /data/www/html:/usr/share/nginx/html -v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /data/nginx/logs:/var/log/nginx nginx #创建新的容器,将配置文件挂载到刚才创建的文件中

    然后在/data/www/html中编辑文件,即可以访问

    安装php

    docker pull php:7.2-fpm #我这里因为框架的问题,只可以使用7.1以上的php,大家可以按照要求切换版本,也可以每个版本都下载下来 docker run --name php-72 -v /data/www/html:/www -d php:7.2-fpm #创建php容器,这里注意挂载的文件目录要和nginx的最好一致

    nginx和php协调配置

    mkdir /data/nginx/conf.d #创建保存nginx配置文件的目录

    在目录中添加test.conf文件,文件内容如下:

    server { listen 80; server_name localhost; location / { root /usr/share/nginx/html; index index.php index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } location ~ \.php$ { fastcgi_pass php:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /www/$fastcgi_script_name; include fastcgi_params; } } docker run --name php-nginx-web -p 80:80 -d \ -v /data/www/html:/usr/share/nginx/html:ro \ -v /data/nginx/conf.d:/etc/nginx/conf.d:ro \ --link php-72:php \ nginx #创建新的nginx和php关联容器,注意其他nginx容器最好关闭,php容器一定要打开

    然后在/data/www/html中书写index.php,就可以正常应用了

    最新回复(0)