How to create environment for multiple Drupal containers using Nginx

0
945
How to create environment for multiple Drupal containers using Nginx

Docker is one of the lightweight virtualization technologies, how to use Docker on its own server, and provide services to all customers is an important learning, this article is mainly about how to use Nginx Reverse Proxy with Docker Container to build a server environment based on Docker.

Basic environment

The environment described here is Ubuntu16.04 and Ubuntu14.04, and the Docker environment has been installed, if you do not install Docker, you can refer to the official teaching article how to install Docker . It can also be used here to provide a good Docker environment.

Required image files

For establish a Drupal environment, its required webserver and database environment separately. Lets see how to build it,

Web Server

Lets pull drupal official image from there official repository. Get it from docker hub.

Syntax:

# docker pull drupal:tag

If you want to use Nginx + Drupal 7.53 server environment,

# docker pull drupal:7.53-fpm

Database

Drupal required database for following operation to perform from webserver SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER. For that lets use commonly used database MySql (You can use anything which you prefer).

Syntax:

# docker pull mysql:tag

Where the Tag can be replaced by the version you want, detailed version please refer here , if you want to download the 5.7.16, you can use the following syntax to download.

# docker pull mysql:5.7.16

Create container

With the image file, of course, we must start to build a container as a basic service Hello. First you need to create a container for the database as a database of Drupal.

Create a Mysql container

Lets create the container using MySql database image,

# docker run --name mysql --restart always -v /home/foxutech/mysql/conf:/etc/mysql/conf.d -v /home/foxutech/mysql/db:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=<<database password>> -d mysql:5.7.16

Where;

  • Name: Can be used for the container to take the name, the name can be used between the container and the container.
  • Restart: This parameter is always the time, you can re-boot time, the container can automatically open, in the container to establish the Drupal environment, this parameter is very important.
  • V: With this parameter, you can synchronize the Mysql database with the profile
  • E: Set Mysql password

Create a Drupal container

As we planned, we are going to launch two different containers for two separate sites. Lets see how to create both container and how to link with MySql database container which we created.

The first Drupal website

# docker run --name drupal1 --link mysql:mysql -p 127.0.0.1:8000:80 -d drupal:7.53-fpm

The second Drupal site

# docker run --name drupal2 --link mysql:mysql -p 127.0.0.1:9000:80 -d drupal:7.53-fpm

Where;

  • Link: This parameter can be two containers linked in the Drupal container, you can directly hit mysql mysql connection to the container IP
  • P: This is the main port of the corresponding port to the container, in order to allow a host, you can connect multiple containers, where the need to match the different port to the container to the 80port. Thus, the native 8000 port corresponds to the first Drupal site’s 80port, and the 9000 port corresponds to the second Drupal site.

Create a Nginx environment

Create Nginx environment to deploy both the site, if doesn’t have one please follow below mentioned steps to perform it.

Install Nginx

# apt-get install software-properties-common python-software-properties  -y
# apt-get install python-software-properties -y
# add-apt-repository ppa:nginx/stable -y
# apt-get update -y
# apt-get upgrade -y
# apt-get install nginx  -y

Create a Nginx Reverse Proxy

Because a server, often need to put more than one site, in the past will use Virtual Host to different Domain to refer to the corresponding site folder, where the use of Nginx Reverse Proxy to deal with different domain refers to the corresponding container.

Create Domain Profile
In Nginx settings, to establish a different Domain configuration file is to be built under [/etc/nginx/conf.d], here because you want to connect to two different sites. So set up two settings files. If the URL is drupal1.tw and drupal2.tw these two sites, the configuration file can be established as drupal1.tw.conf and drupal2.tw.conf .

Profile Content Write

  • Drupal1.tw.conf content
server {
    listen 80;
    server_name drupal1.tw;
    proxy_connect_timeout       300;
    proxy_send_timeout          300;
    proxy_read_timeout          300;
    send_timeout                300;
    location ~ / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;
        proxy_pass http://127.0.0.1:8000;
    }
}
  • Drupal2.tw.conf content
server {
    listen 80;
    server_name drupal2.tw;
    proxy_connect_timeout       300;
    proxy_send_timeout          300;
    proxy_read_timeout          300;
    send_timeout                300;
    location ~ / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;
        proxy_pass http://127.0.0.1:9000;
    }
}

Reboot Nginx

# service nginx reload

Once restarted nginx service, now your both the domain get forwarded to appropriate containers.

NO COMMENTS