How to deploy several websites on one host with D2C.io

This guide helps you to deploy multiple websites on one host using the D2C platform and stacks from our Stackhub. 

In the first example, I’ll take a WordPress light stack and deploy it twice. As a result, the configuration looks like: 

Denis Zaripov

Product manager

In the second, I’ll deploy another website using the same database. The configuration is like:

Example 1. Preparing

I’ve already deployed a stack WordPress light stack and forked the stack’s repository. 

You can create your own fork and do the same or use mine. All we need to do with a fork is to turn off remoteAcess of an edge service (HAProxy balancer in this case) and Adminer:

  - name: balancer
    type: haproxy
    version: latest
    ports:
      - 80
      - 443
    remoteAccess: false
    serviceFiles:
      - name: blog
        sticky: true
    deployTo:
      - mainwp
      
  - name: adminer
    type: docker
    image: adminer
    version: latest
    ports:
      - 8080
    remoteAccess: false
    env:
      ADMINER_DEFAULT_SERVER: "{{=service('db').getMasterAlias()}}"
    deployTo:
      - mainwp

Then, we need to disable Internet Access for these services in the current project. 

Open HAProxy and Adminer services → Edit settings → Disable Internet Access

Creating the next stack

Let’s create the second stack.

Create a new project → click “Import stack” → paste a link to your fork (or use this link to deploy mine) → click “Import from link

Choose “Select existing hosts” tab and choose your host.

Then fill in the necessary form like root and user password for a database service and environment variables for a blog service.

Now we can click “Create hosts and services“. All the services will be deployed

Creating NGINX service

The last thing we need to do is to create an NGINX service. It can serve any amount of services, unlike HAProxy (balancer) in D2C. It requires open 88 and 443 ports, and on our host they are free because we closed them before

Let’s open any project, then click “+Create service” and find an NGINX bar.

After that, we need create configs for our services. Find configure block, click “+Add service config” and choose a balancer. 

Click “Generate config“.

The config is generated automatically. 

At this step, you can set up your own domains and get free HTTPS Let’s Encrypt certificate. Be aware that DNS-record for your domains with the same IP-address as a host of NGINX should be accessible at this moment.

Now, we need to do the same for the adminer. 

As I mentioned before, NGINX can serve any amount of services as well as from other projects. So, we now can do the same for balancer and adminer from the second project.

Ok, we added all services to NGINX and now can deploy it.

Now, we can see links at services cards and reach them via a browser.

You can edit NGINX configs in the future and your domains after all the tests. Read more about creating NGINX service in the special post. 

Example 2. Creating a new website with the same database

Sometimes for several small projects, you want to have one database. Let’s review this example. We already have a deployed database service (MariaDB). 

Creating a new database and username

At first, we need to create a new database in the current MariaDB. For this purposes, you can use any things you prefer, like Adminer (which is already in the stack), phpMyAdmin, a terminal, etc.

I use Adminer and show an example with it. After login, click “Create database“.

Give it a name, choose a collation and click “Save“. 

The new database is created. You can use the username which is already created for this database service. I’ll create a new one. So, I move to privileges. 

Then, specify a new username, password and grant all privileges to it. 

Ok, a new username is created.

Clone Redis service

Next, I’m going to clone services. In our WordPress stacks, we use Redis and install plugin for it. So, I clone it at first. 

Open a Redis service (named cache in the stack) → click “Clone” → Choose a new project

Give it a name and click ok.

A creating service page with the same settings as original service will be opened. In Redis we do not need to change anything. Just choose a host and click “Create service“.

Clone WordPress service

The process is almost the same, but we need to change some settings. 

As with Redis, I start with opening WordPress (Docker, named blog in the stack) service and clone it to a new project.

Give it a name and click OK.

Then, we need to change Initial commands and change the path to /var/www/newwp (the name you use for your new service)
cmd wp core install --url=newwp.iugu74tb2.at.d2c.io --title='"$WP_TITLE"' --admin_user=$WP_ADMIN_USERNAME --admin_password=$WP_ADMIN_PASSWORD --admin_email=$WP_ADMIN_EMAIL --path=/var/www/newwp
cmd wp plugin install redis-cache --activate --path=/var/www/newwp
cmd wp redis enable --path=/var/www/newwp
cmd wp plugin install nginx-helper --activate --path=/var/www/newwp
echo '{"enable_purge":"1","cache_method":"enable_fastcgi","purge_method":"unlink_files","enable_map":null,"enable_log":null,"log_level":"INFO","log_filesize":"5","enable_stamp":null,"purge_homepage_on_edit":"1","purge_homepage_on_del":"1","purge_archive_on_edit":"1","purge_archive_on_del":"1","purge_archive_on_new_comment":null,"purge_archive_on_deleted_comment":null,"purge_page_on_mod":"1","purge_page_on_new_comment":"1","purge_page_on_deleted_comment":"1","redis_hostname":"cache","redis_port":"6379","redis_prefix":"nginx-cache:","purge_url":"","redis_enabled_by_constant":0}' | cmd wp option add rt_wp_nginx_helper_options --format=json --path=/var/www/newwp


Then I change the first environment variable SERVICE_NAME to newwp. At this step, you can also change credentials for your new website.

After that, I specify a new database in wp-config.php. Move to “Configs” block and change settings for a database and Redis:

/** The name of the database for WordPress */
define('DB_NAME', 'newwpbase');

/** MySQL database username */
define('DB_USER', 'newwordpress');

/** MySQL database password */
define('DB_PASSWORD', 'password');
/** Define Redis host */
define('WP_REDIS_HOST', 'redis');

Redis host is the name you give to a service. You can find it at a container card at a service page.

Click “Create service” now, and a new WordPress is deployed to a host. 

Create NGINX config

The last thing is to create a config in NGINX for a new WordPress service. The process is absolutely the same:

NGINX service → Edit settings → Configure service block → +Add service config → Generate config → Click “Update service“.

The new WordPress website is now reachable via a special link.

Summary

In this guide, we reviewed:

  • how to deploy the same stack on the same host
  • how to create an NGINX service and add service configs to it
  • how to create a new database using Adminer
  • how to clone services
  • how to create a new website using the same database service
 

I hope it was helpful for you. Let us know if you want to know other things which you can do with D2C using the form in contacts or reach us in Discord.