Linux 安装 Nginx

Linux 下安装 Nginx

一、获取镜像

docker pull nginx

二、启动镜像

docker run -p 8888:80 -d nginx

验证:http://xx.xxx.xx.xxxx:8888

三、配置文件外置

在主机上创建目录/disk2/docker/nginx,在该目录下创建conf,logs,www三个目录

在/disk2/docker/nginx/conf目录下新增文件default.conf,写入如下内容,

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    access_log  /var/log/nginx/access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/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's document root
    # concurs with nginx's one
    #
    #location ~ /.ht {
    #    deny  all;
    #}
}

在/disk2/docker/nginx/www目录下新增文件index.html,写入如下内容:

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx in docker!</title>
<body>
<h1>docker!!!!</h1>
</body>
</html>

四、挂载外部配置文件启动

 docker run -p 8888:80 -v /disk2/docker/nginx/conf:/etc/nginx/conf.d -v /disk2/docker/nginx/www:/usr/share/nginx/html -v /disk2/docker/nginx/logs:/var/log/nginx -d nginx

验证:http://xx.xxx.xx.xxxx:8888

参考资料:

linux上docker部署nginx

原文地址:https://www.cnblogs.com/wangwangfei/p/13524070.html