-
Notifications
You must be signed in to change notification settings - Fork 42
/
README.Rmd
145 lines (100 loc) · 4.64 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
```
# Host Multiple HTTPS Websites on One Server
provides a template to easily configure the deployement of multiple websites on a single server. Reverse-proxy, nginx configuration files and SSL certificate are created automatically for each website running in a Docker cntainer.
A detailed explanation is provided at: [How To Host Multiple HTTPS Websites on One Server](https://www.datanovia.com/en/lessons/how-host-multiple-https-websites-on-one-server/)
## Prerequisites
### Install required tools and create domain names
- Git, docker and docker-compose are installed on your server
- Several websites run inside Docker containers on a single server. (Each one could either be a static files server, or Wordpress running on Apache, etc.
- The domain name for each website is configured to point to the IP of the server. Your host must be publicly reachable on both port `80` and `443`.Check your firewall rules to make sure that these ports are open.
### Create websites directories
```bash
# 0. settings
web_dir=/srv/www
myusername=kassambara
# 1. Create the website directory
sudo mkdir -p $web_dir
# 2. set your user as the owner
sudo chown -R $myusername $web_dir
# 3. set the web server as the group owner
sudo chgrp -R www-data $web_dir
# 4. 755 permissions for everything
sudo chmod -R 755 $web_dir
# 5. New files and folders inherit
# group ownership from the parent folder
chmod g+s $web_dir
```
## Project structure
**Download a template into your website directories www**:
```bash
web_dir=/srv/www
git clone https://github.com/kassambara/nginx-multiple-https-websites-on-one-server $web_dir
```
Inside `/nginx-proxy`, there are four empty directories: `conf.d`, `vhost.d`, `html` and `certs`. These are used to store the nginx and the Let’s Encrypt configuration files.
**Download the latest updated version of [nginx.tmpl](https://raw.githubusercontent.com/jwilder/nginx-proxy/master/nginx.tmpl)**:
```bash
curl -s https://raw.githubusercontent.com/jwilder/nginx-proxy/master/nginx.tmpl> $web_dir/nginx-proxy/nginx.tmpl
````
## Run the nginx reverse proxy
```bash
# 1. Create the docker network
docker network create nginx-proxy
# 2. Create the reverse proxy with the
# nginx, nginx-gen and nginx-letsencrypt containers
cd /srv/www/nginx-proxy/
docker-compose up -d
```
## Link a website to the running nginx-proxy
The `docker-compose.yml` file of the website, you want to link, should include the following instructions provided in the template available in the folder `your-website-one.com` (**not** the one from nginx-proxy above). The content of the template looks like this:
```yaml
version: '3.6'
services:
my-app:
image: nginx
restart: always
environment:
# NGINX-PROXY ENVIRONMENT VARIABLES: UPDATE ME
- VIRTUAL_HOST=your-website-one.com
- VIRTUAL_PORT=80
- LETSENCRYPT_HOST=your-website-one.com
# END NGINX-PROXY ENVIRONMENT VARIABLES
expose:
- 80
networks:
default:
external:
name: nginx-proxy
```
1. **Environment variables**:
- `VIRTUAL_HOST`: your domain name, used in the nginx configuration.
- `VIRTUAL_PORT`: (optional) the port your website is listening to (default to 80).
- `LETSENCRYPT_HOST`: your domain name, used in the Let’s Encrypt configuration.
- `LETSENCRYPT_EMAIL`: your email, used in the Let’s Encrypt configuration.
2. **Ports**:
- the exposed port (here 80) should be the same as the `VIRTUAL_PORT` above.
3. **Network**:
- your website container should be linked to the external docker network named `nginx-proxy`
```
Once the update of the `docker-compose.yml` file is done, you can **start the website with**:
```bash
cd /srv/www/your-website-one.com
docker-compose up -d
```
```{block, type = "success"}
The website is automatically detected by the reverse proxy, has a HTTPS certificate and is visible at https://your-website-one.com.
You can repeat this last step for any other container you want to proxy
```
## References
- [Host multiple websites with HTTPS on a single server](https://medium.com/@francoisromain/host-multiple-websites-with-https-inside-docker-containers-on-a-single-server-18467484ab95)
- [Automated nginx proxy for Docker containers using docker-gen](https://github.com/jwilder/nginx-proxy)
- [LetsEncrypt companion container for nginx-proxy](https://github.com/JrCs/docker-letsencrypt-nginx-proxy-companion)