Home  >  Article  >  Operation and Maintenance  >  How to deploy Nginx on Docker

How to deploy Nginx on Docker

王林
王林forward
2023-05-11 18:28:183038browse

1. Download the Nginx image from docker

docker pull nginx

2. Create the mounting directory

and put the files here. Map the directory corresponding to Nginx in docker without changing it. The file has entered the container

mkdir -p /data/nginx/{conf,conf.d,html,logs}

3. In order to ensure the correctness of the file, it is recommended to enter the container first and copy the corresponding file.

If it is inconvenient, you can open two windows and enter one Go to the container, copy the left side to the right side, this is to ensure that the file is correct

#启动容器
docker run -itd nginx /bin/bash
#进入容器
docker attach xxxxxxxxxx
##Configuration filenginx.conf/data/nginx/conf/nginx.conf/etc/nginx/nginx.confConfiguration file folderconf.d folder/data/nginx/conf. d/etc/nginx/conf.dHomepage folder html pathhtml folder/data/nginx/ html/usr/share/nginx/htmlLog filelog folder/data/nginx/logs /var/log/nginx
Instructions File Mounting path nginx path
#This is the corresponding mounting directory. Copy the nginx.conf file and default.conf in conf.d Go to the corresponding folder and put it in, and then modify it

4. Next, just modify the default.conf file

The most I can do here is change the port number, access path, etc.

server {
 
    #端口号
    listen       80;
    #定义使用 localhost 访问
    server_name  localhost;
 
    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;
 
    location / {
        #根目录位置
        root   /usr/share/nginx/html;
        #index 文件位置
        index  1.html;
    }
 
    #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;
    #}
}

The 1.html used for testing here is written by myself

<html>
<head>
<title>Mynginx</title>
</head>
<body>
<h2>
欢迎使用nginx!
</h2>
</body>
</html>

5. Next, you can start the container

docker run  --name myNginx -d -p 8089:80 -v /data/nginx/html:/usr/share/nginx/html -v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /data/nginx/conf.d:/etc/nginx/conf.d  -v /data/nginx/logs:/var/log/nginx nginx

The mounting path must be correct, don’t write Wrong

-p 8089:80 Here, map port 80 to port 8089 of the host, so that the access is port 8089. There is no need to change the default port of nginx

You can take a look next Whether the container starts normally

docker ps

If you don’t see the container, it means there is a problem with the startup. Check whether the configuration file is written incorrectly or the mounting path is incorrect.

You can directly start it after starting it. Browser localhost:8089 I saw the 1.index page I just wrote

6. Update the configuration file without stopping nginx

When we modify the configuration file, we need to update the configuration file. At this time, open two The window is very cool

#进入容器
docker exec -it xxxxxxxxxxx /bin/bash
 
#测试配置文件是否有问题
nginx -t
 
#要是显示 successful 就可以更新了
nginx -s reload

The above is the detailed content of How to deploy Nginx on Docker. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete