首页 运维 nginx CentOS7 Docker Nginx部署及运行的方法

CentOS7 Docker Nginx部署及运行的方法

May 21, 2023 pm 06:19 PM
docker nginx centos7

1、资源准备

dockerfile文件

# "ported" by adam miller <maxamillion@fedoraproject.org> from 
#  https://github.com/fedora-cloud/fedora-dockerfiles 
# 
# originally written for fedora-dockerfiles by 
#  scollier <scollier@redhat.com> 
 
from centos:centos7 
maintainer the centos project <cloud-ops@centos.org> 
 
run yum -y update; yum clean all 
run yum -y install epel-release tar ; yum clean all 
run yum -y install nginx ; yum clean all 
add nginx.conf /opt/deploy/nginx/nginx.conf 
run echo "daemon off;" >> /opt/deploy/nginx/nginx.conf 
#run curl https://git.centos.org/sources/httpd/c7/acf5cccf4afaecf3afeb18c50ae59fd5c6504910 \ 
#  | tar -xz -c /usr/local/nginx/html \ 
#  --strip-components=1 
#run sed -i -e &#39;s/apache/nginx/g&#39; -e &#39;/apache_pb.gif/d&#39; \  
#  /usr/local/nginx/html/index.html 
 
expose 80 
 
#cmd [ "/usr/local/nginx/sbin" ]
登录后复制

注意:路径需要在系统上面存在以及对应

nginx.conf文件

# for more information on configuration, see: 
#  * official english documentation: http://nginx.org/en/docs/ 
#  * official russian documentation: http://nginx.org/ru/docs/ 
 
user nginx; 
worker_processes 1; 
 
error_log /usr/logs/nginx/error.log; 
#error_log /var/log/nginx/error.log notice; 
#error_log /var/log/nginx/error.log info; 
 
pid    /run/nginx.pid; 
 
 
events { 
  worker_connections 1024; 
} 
 
 
http { 
  include    mime.types; 
  default_type application/octet-stream; 
 
  log_format main &#39;$remote_addr - $remote_user [$time_local] "$request" &#39; 
           &#39;$status $body_bytes_sent "$http_referer" &#39; 
           &#39;"$http_user_agent" "$http_x_forwarded_for"&#39;; 
 
  access_log /usr/logs/nginx/access.log main; 
 
  sendfile    on; 
  #tcp_nopush   on; 
 
  #keepalive_timeout 0; 
  keepalive_timeout 65; 
 
  #gzip on; 
 
  # load modular configuration files from the /etc/nginx/conf.d directory. 
  # see http://nginx.org/en/docs/ngx_core_module.html#include 
  # for more information. 
  #include /etc/nginx/conf.d/*.conf; 
 
  index  index.html index.htm; 
 
  server { 
    listen    80; 
    server_name localhost; 
    root     /usr/share/nginx/html; 
 
    #charset koi8-r; 
 
    #access_log /var/log/nginx/host.access.log main; 
 
    location / { 
      autoindex on; 
    } 
 
    # redirect server error pages to the static page /40x.html 
    # 
    error_page 404       /404.html; 
    location = /40x.html { 
    } 
 
    # redirect server error pages to the static page /50x.html 
    # 
    error_page  500 502 503 504 /50x.html; 
    location = /50x.html { 
    } 
 
    # proxy the php scripts to apache listening on 127.0.0.1:80 
    # 
    #location ~ \.php$ { 
    #  proxy_pass  http://127.0.0.1; 
    #} 
 
    # pass the php scripts to fastcgi server listening on 127.0.0.1:9000 
    # 
    #location ~ \.php$ { 
    #  root      html; 
    #  fastcgi_pass  127.0.0.1:9000; 
    #  fastcgi_index index.php; 
    #  fastcgi_param script_filename /scripts$fastcgi_script_name; 
    #  include    fastcgi_params; 
    #} 
 
    # deny access to .htaccess files, if apache&#39;s document root 
    # concurs with nginx&#39;s one 
    # 
    #location ~ /\.ht { 
    #  deny all; 
    #} 
  } 
 
 
  # another virtual host using mix of ip-, name-, and port-based configuration 
  # 
  #server { 
  #  listen    8000; 
  #  listen    somename:8080; 
  #  server_name somename alias another.alias; 
  #  root     html; 
 
  #  location / { 
  #  } 
  #} 
 
 
  # https server 
  # 
  #server { 
  #  listen    443; 
  #  server_name localhost; 
  #  root     html; 
 
  #  ssl         on; 
  #  ssl_certificate   cert.pem; 
  #  ssl_certificate_key cert.key; 
 
  #  ssl_session_timeout 5m; 
 
  #  ssl_protocols sslv2 sslv3 tlsv1; 
  #  ssl_ciphers high:!anull:!md5; 
  #  ssl_prefer_server_ciphers  on; 
 
  #  location / { 
  #  } 
  #} 
 
}
登录后复制

注意:路径需要在系统上面存在以及对应

2、执行构建镜像命令

复制代码 代码如下:


[root@localhost nginx]# sudo docker build --rm --tag os7/nginx:centos7 . 

执行结果截图:

CentOS7 Docker Nginx部署及运行的方法

3、查看镜像是否安装构建成功 docker images

CentOS7 Docker Nginx部署及运行的方法

4、创建容器 docker run -i -t -d -p 192.168.32.129:81:80 os7/nginx /bin/bash

注意:192.168.32.129这个ip的话,则需要在/etc/hosts中添加

192.168.32.129     localhost

5、查看容器是否创建成功并启动 docker ps

CentOS7 Docker Nginx部署及运行的方法

6、测试是否成功访问 curl http://192.168.32.129:81

CentOS7 Docker Nginx部署及运行的方法

会出现这个拒绝连接,那怎么办呢?有办法解决的,我们先进入该容器里面

7、进入容器 docker exec -i -t small_hodgkin /bin/sh

CentOS7 Docker Nginx部署及运行的方法

8、接着在容器里面执行(直接输入即可)

nginx

9、在容器外面执行 curl http://192.168.32.129:81

CentOS7 Docker Nginx部署及运行的方法

成功了。

10、再到虚拟机外面通过浏览器访问

CentOS7 Docker Nginx部署及运行的方法

以上是CentOS7 Docker Nginx部署及运行的方法的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
4 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

Pi Node教学:什么是Pi节点?如何安装和设定Pi Node? Pi Node教学:什么是Pi节点?如何安装和设定Pi Node? Mar 05, 2025 pm 05:57 PM

PiNetwork节点详解及安装指南本文将详细介绍PiNetwork生态系统中的关键角色——Pi节点,并提供安装和配置的完整步骤。Pi节点在PiNetwork区块链测试网推出后,成为众多先锋积极参与测试的重要环节,为即将到来的主网发布做准备。如果您还不了解PiNetwork,请参考Pi币是什么?上市价格多少?Pi用途、挖矿及安全性分析。什么是PiNetwork?PiNetwork项目始于2019年,拥有其专属加密货币Pi币。该项目旨在创建一个人人可参与

deepseek怎么安装 deepseek怎么安装 Feb 19, 2025 pm 05:48 PM

DeepSeek的安装方法有多种,包括:从源码编译(适用于经验丰富的开发者)使用预编译包(适用于Windows用户)使用Docker容器(最便捷,无需担心兼容性)无论选择哪种方法,请仔细阅读官方文档并充分准备,避免不必要的麻烦。

使用Docker Container部署JavaEE应用程序 使用Docker Container部署JavaEE应用程序 Jun 05, 2024 pm 08:29 PM

使用Docker容器部署JavaEE应用程序:创建Dockerfile定义镜像、构建镜像、运行容器并映射端口,然后在浏览器中访问应用程序。示例JavaEE应用程序:RESTAPI与数据库交互,通过Docker部署后可在localhost访问。

vscode怎么安装Docker扩展 vscode安装Docker扩展的步骤 vscode怎么安装Docker扩展 vscode安装Docker扩展的步骤 May 09, 2024 pm 03:25 PM

1.首先,打开界面后,点击左侧的扩展图标按钮2.随后,在打开的扩展页面中找到搜索栏位置3.接着,鼠标输入Docker字眼查找扩展插件4.最后,选中目标插件,点击右下角的安装按钮即可

WordPress站点文件访问受限:为什么我的.txt文件无法通过域名访问? WordPress站点文件访问受限:为什么我的.txt文件无法通过域名访问? Apr 01, 2025 pm 03:00 PM

Wordpress站点文件访问受限:排查.txt文件无法访问的原因最近有用户在配置小程序业务域名时,遇到一个问题:�...

在Docker环境中使用PECL安装扩展时为什么会报错?如何解决? 在Docker环境中使用PECL安装扩展时为什么会报错?如何解决? Apr 01, 2025 pm 03:06 PM

在Docker环境中使用PECL安装扩展时报错的原因及解决方法在使用Docker环境时,我们常常会遇到一些令人头疼的问�...

如何在同一台服务器上通过Nginx配置让PHP5.6和PHP7共存? 如何在同一台服务器上通过Nginx配置让PHP5.6和PHP7共存? Apr 01, 2025 pm 03:15 PM

在同一系统中同时运行多个PHP版本是一个常见的需求,特别是当不同项目依赖于不同版本的PHP时。如何在同一台...

h5项目怎么运行 h5项目怎么运行 Apr 06, 2025 pm 12:21 PM

运行 H5 项目需要以下步骤:安装 Web 服务器、Node.js、开发工具等必要工具。搭建开发环境,创建项目文件夹、初始化项目、编写代码。启动开发服务器,使用命令行运行命令。在浏览器中预览项目,输入开发服务器 URL。发布项目,优化代码、部署项目、设置 Web 服务器配置。

See all articles