Docker + Nginx!

preface

Ok guys, we already have the server, assuming our project is already developed locally, we’re ready to go!


First, a wave of Dockers

First of all, what is a Docker? Here is the introduction of Baidu Baike:

Easy to understand

Actually everybody can understand it, we can be on the same server install a lot of images, is like a computer can install Windows 7, at the same time win10 system such as multiple system, such as image you can understand for the system, while the docker allows us to run on the server multiple images, but also can build his own image, Like minecraft, you can download a map to play, create a map and save it for yourself or give it to someone else, or open multiple Windows to play multiple maps at the same time.

Three concepts

There are three concepts in Docker: image, container and repository. The image, needless to say, will form a container when it runs. The image can be pulled from the remote Dockerhub repository, uploaded, or saved in its own local repository.

Why use it?

The advantage of using Docker is that we can separate multiple images of various environments to run in multiple containers at the same time. For example, Jenkins runs in one container and Nginx runs in another container. Although they cannot be directly accessed, they can control some system files and directories at the same time. This can do communication, but also can generate some configured our environment directly into a mirror, can use at any time by then, we bought a server, for example, there is no need to configure again to install various environment, and put on a server in the Docker build image to get the server running is ok?

Separating the management image containers allows us to be more focused (not to mention the benefits of modularity), and the reuse of images allows us to reduce a lot of repetitive operations.

In that case, Docker is really a pretty good guy

However, we have to consider whether to add Docker to the automatic deployment, because it does take up a bit of memory and our beggar edition server runs a bit slow, although it is not possible to use, but in the spirit of learning attitude, we still accept it.


The use of the Docker

Ok, first of all, let’s install a Docker on our server, because we choose a centOS system, so it is very simple to install Docker.

Very perfunctory installation tutorial

Here is to throw you a rookie tutorial link, we follow the link to install it.

Then run a wave of commands to see if it is installed:

docker
Copy the code

After running the command, you can see some help information of Docker, which indicates that we have installed Docker.

Some simple Docker operations

Here’s a quick overview of how docker works:

  1. View the current mirrorsdocker images
  2. Run to delete an unwanted imagedocker rm <image_id>Image_id can be found in the previous step when viewing the image
  3. You can also usedocker build  <image_tag>To build an image, which is almost unnecessary for beginners
  4. Pull the mirrordocker pull <image>
  5. Run an image in a container is also very simple, note, do not need to run a container, will automatically allocate a container, and run if there is no image will automatically pull the image, such as:docker run <image>
  6. View the running containerdocker psView all containersdocker ps -a
  7. Start the containerdocker start <container_id>Stop the containerdocker stop <container_id>Remove the containerdocker rm <container_id>

Well, these commands are basically enough for us to use, and we’ll see if we don’t.


Start an Nginx WEB server

For the next step, let’s build an Nginx server for web projects

Pull an Nginx image and let it run

First pull and run an nginx image from docker:

docker pull nginx
docker run --name nginx-server -p 4000:80 -d nginx
Copy the code

Nginx-server: / / nginx-server: / / nginx-server: / / nginx-server: / / nginx-server: / / nginx-server: / / nginx-server: / / nginx-server: / / nginx-server: / / nginx-server: / / nginx-server: / / nginx-server: / / When we access port 4000 of the cloud server, we are accessing port 80 of the Nginx service.

Check to see if it works:

dicker ps
Copy the code

Good, it’s up and running, and you should be able to see it next by going to http://IP :4000/.


Various directories and configuration file processing

Ok, there are a few things to do: First, let’s create a directory to manage nginx configuration and resource files and things like that. I’ll create it directly under root.

mkdir -p ~/nginx/www/react-mixture ~/nginx/logs ~/nginx/conf
Copy the code

There are three folders, the WWW folder is for HTML, CSS, JS subclass resource files, the React -mixture is ready to put a React project. Log and access.log can be used to check nginx service error logs and passed request logs. Conf directory is ready to store configuration files.

Then pull the default configuration of the nginx image we pulled into the folder we created:

Docker cp < container ID >:/etc/nginx/nginx.conf ~/nginx/confCopy the code

The nginx image is configured in /etc/nginx/nginx.conf by default, and we took it to our own conf directory.

Docker rm < container id> docker rm < container id> docker rm < container id>

docker run -d -p 4000:80 --name nginx-server -v ~/nginx/www:/usr/share/nginx/html -v ~/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v ~/nginx/logs:/var/log/nginx nginx
Copy the code

– d – p – name everyone already knows that behind the -v command is to create our own directory mounted to the container corresponding directory, create your own directory and the directory of container with: The nginx directory is mapped to the nginx directory, and all resources within the nginx container are synchronized.

/ /nginx/ WWW, and see if nginx can access it. Assuming we have built the index.html file, remember:

  1. Open the terminal in the directory where you created index.html
  2. Enter in the terminalSCP -r index.html <username eq:root> '[@](mailto:[email protected]) IP address :~/nginx/ WWW

Ok, check the file in the server has appeared, this is a convenient way to upload the local file to the server yo, then visit http://ip address :4000 can access this page, is not very cool it.

Ok, wow, just found that I said so much ok, don’t know you ok ok, haha, then we will go to a configured nginx. Conf file and upload the past, this file is in nginx configuration server path, the request agent and so on.

scp -r nginx.conf <username eq:root>@ipAddress: ~ / nginx/confCopy the code

A simple Nginx configuration file

By the way, my configuration is very simple, long like this, you go to your own ha, not to learn:

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local]. ""$request"'
                      '$status $body_bytes_sent "$http_referer"'
                      '"$http_user_agent""$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush on;

    keepalive_timeout  65;

    #gzip on;

    server {
        listen       80;
        server_name  47.92.164.250;

        #charset koi8-r;

        #access_log logs/host.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   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        location /api-lottery/ {
           proxy_pass   http://apis.juhe.cn/lottery/;
        }

        # 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;
        #}
    }

    include /etc/nginx/conf.d/*.conf;
}
Copy the code

If not, stop the container, start it again, and wait. The configuration changes will be delayed.


After the language

Nginx/WWW /react-mixture can be accessed via http://ip address :4000/react-mixture.