Skip to content

Commit c3bef28

Browse files
authored
Merge pull request #1547 from jc21/makes-sqlite-default
Changes documentation to make SQLite the default db
2 parents 3b47dec + efc5bff commit c3bef28

File tree

3 files changed

+119
-161
lines changed

3 files changed

+119
-161
lines changed

README.md

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -74,28 +74,12 @@ services:
7474
- '80:80'
7575
- '81:81'
7676
- '443:443'
77-
environment:
78-
DB_MYSQL_HOST: "db"
79-
DB_MYSQL_PORT: 3306
80-
DB_MYSQL_USER: "npm"
81-
DB_MYSQL_PASSWORD: "npm"
82-
DB_MYSQL_NAME: "npm"
8377
volumes:
8478
- ./data:/data
8579
- ./letsencrypt:/etc/letsencrypt
86-
db:
87-
image: 'jc21/mariadb-aria:latest'
88-
restart: unless-stopped
89-
environment:
90-
MYSQL_ROOT_PASSWORD: 'npm'
91-
MYSQL_DATABASE: 'npm'
92-
MYSQL_USER: 'npm'
93-
MYSQL_PASSWORD: 'npm'
94-
volumes:
95-
- ./data/mysql:/var/lib/mysql
9680
```
9781
98-
3. Bring up your stack
82+
3. Bring up your stack by running
9983
10084
```bash
10185
docker-compose up -d

backend/index.js

Lines changed: 69 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -44,84 +44,85 @@ async function appStart () {
4444

4545
async function createDbConfigFromEnvironment() {
4646
return new Promise((resolve, reject) => {
47-
const envMysqlHost = process.env.DB_MYSQL_HOST || null;
48-
const envMysqlPort = process.env.DB_MYSQL_PORT || null;
49-
const envMysqlUser = process.env.DB_MYSQL_USER || null;
50-
const envMysqlName = process.env.DB_MYSQL_NAME || null;
51-
const envSqliteFile = process.env.DB_SQLITE_FILE || null;
52-
53-
if ((envMysqlHost && envMysqlPort && envMysqlUser && envMysqlName) || envSqliteFile) {
54-
const fs = require('fs');
55-
const filename = (process.env.NODE_CONFIG_DIR || './config') + '/' + (process.env.NODE_ENV || 'default') + '.json';
56-
let configData = {};
57-
58-
try {
59-
configData = require(filename);
60-
} catch (err) {
61-
// do nothing
62-
}
47+
const envMysqlHost = process.env.DB_MYSQL_HOST || null;
48+
const envMysqlPort = process.env.DB_MYSQL_PORT || null;
49+
const envMysqlUser = process.env.DB_MYSQL_USER || null;
50+
const envMysqlName = process.env.DB_MYSQL_NAME || null;
51+
let envSqliteFile = process.env.DB_SQLITE_FILE || null;
52+
53+
const fs = require('fs');
54+
const filename = (process.env.NODE_CONFIG_DIR || './config') + '/' + (process.env.NODE_ENV || 'default') + '.json';
55+
let configData = {};
56+
57+
try {
58+
configData = require(filename);
59+
} catch (err) {
60+
// do nothing
61+
}
62+
63+
if (configData.database && configData.database.engine && !configData.database.fromEnv) {
64+
logger.info('Manual db configuration already exists, skipping config creation from environment variables');
65+
resolve();
66+
return;
67+
}
68+
69+
if ((!envMysqlHost || !envMysqlPort || !envMysqlUser || !envMysqlName) && !envSqliteFile){
70+
envSqliteFile = '/data/database.sqlite';
71+
logger.info(`No valid environment variables for database provided, using default SQLite file '${envSqliteFile}'`);
72+
}
6373

64-
if (configData.database && configData.database.engine && !configData.database.fromEnv) {
65-
logger.info('Manual db configuration already exists, skipping config creation from environment variables');
74+
if (envMysqlHost && envMysqlPort && envMysqlUser && envMysqlName) {
75+
const newConfig = {
76+
fromEnv: true,
77+
engine: 'mysql',
78+
host: envMysqlHost,
79+
port: envMysqlPort,
80+
user: envMysqlUser,
81+
password: process.env.DB_MYSQL_PASSWORD,
82+
name: envMysqlName,
83+
};
84+
85+
if (JSON.stringify(configData.database) === JSON.stringify(newConfig)) {
86+
// Config is unchanged, skip overwrite
6687
resolve();
6788
return;
6889
}
6990

70-
if (envMysqlHost && envMysqlPort && envMysqlUser && envMysqlName) {
71-
const newConfig = {
72-
fromEnv: true,
73-
engine: 'mysql',
74-
host: envMysqlHost,
75-
port: envMysqlPort,
76-
user: envMysqlUser,
77-
password: process.env.DB_MYSQL_PASSWORD,
78-
name: envMysqlName,
79-
};
80-
81-
if (JSON.stringify(configData.database) === JSON.stringify(newConfig)) {
82-
// Config is unchanged, skip overwrite
83-
resolve();
84-
return;
85-
}
86-
87-
logger.info('Generating MySQL db configuration from environment variables');
88-
configData.database = newConfig;
91+
logger.info('Generating MySQL knex configuration from environment variables');
92+
configData.database = newConfig;
8993

90-
} else {
91-
const newConfig = {
92-
fromEnv: true,
93-
engine: 'knex-native',
94-
knex: {
95-
client: 'sqlite3',
96-
connection: {
97-
filename: envSqliteFile
98-
},
99-
useNullAsDefault: true
100-
}
101-
};
102-
if (JSON.stringify(configData.database) === JSON.stringify(newConfig)) {
103-
// Config is unchanged, skip overwrite
104-
resolve();
105-
return;
94+
} else {
95+
const newConfig = {
96+
fromEnv: true,
97+
engine: 'knex-native',
98+
knex: {
99+
client: 'sqlite3',
100+
connection: {
101+
filename: envSqliteFile
102+
},
103+
useNullAsDefault: true
106104
}
107-
108-
logger.info('Generating Sqlite db configuration from environment variables');
109-
configData.database = newConfig;
105+
};
106+
if (JSON.stringify(configData.database) === JSON.stringify(newConfig)) {
107+
// Config is unchanged, skip overwrite
108+
resolve();
109+
return;
110110
}
111111

112-
// Write config
113-
fs.writeFile(filename, JSON.stringify(configData, null, 2), (err) => {
114-
if (err) {
115-
logger.error('Could not write db config to config file: ' + filename);
116-
reject(err);
117-
} else {
118-
logger.info('Wrote db configuration to config file: ' + filename);
119-
resolve();
120-
}
121-
});
122-
} else {
123-
resolve();
112+
logger.info('Generating SQLite knex configuration');
113+
configData.database = newConfig;
124114
}
115+
116+
// Write config
117+
fs.writeFile(filename, JSON.stringify(configData, null, 2), (err) => {
118+
if (err) {
119+
logger.error('Could not write db config to config file: ' + filename);
120+
reject(err);
121+
} else {
122+
logger.debug('Wrote db configuration to config file: ' + filename);
123+
resolve();
124+
}
125+
});
125126
});
126127
}
127128

docs/setup/README.md

Lines changed: 49 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,44 @@
11
# Full Setup Instructions
22

3-
## MySQL Database
3+
## Running the App
4+
5+
Create a `docker-compose.yml` file:
6+
7+
```yml
8+
version: "3"
9+
services:
10+
app:
11+
image: 'jc21/nginx-proxy-manager:latest'
12+
restart: unless-stopped
13+
ports:
14+
# These ports are in format <host-port>:<container-port>
15+
- '80:80' # Public HTTP Port
16+
- '443:443' # Public HTTPS Port
17+
- '81:81' # Admin Web Port
18+
# Add any other Stream port you want to expose
19+
# - '21:21' # FTP
20+
21+
# Uncomment the next line if you uncomment anything in the section
22+
# environment:
23+
# Uncomment this if you want to change the location of
24+
# the SQLite DB file within the container
25+
# DB_SQLITE_FILE: "/data/database.sqlite"
26+
27+
# Uncomment this if IPv6 is not enabled on your host
28+
# DISABLE_IPV6: 'true'
29+
30+
volumes:
31+
- ./data:/data
32+
- ./letsencrypt:/etc/letsencrypt
33+
```
34+
35+
Then:
36+
37+
```bash
38+
docker-compose up -d
39+
```
40+
41+
## Using MySQL / MariaDB Database
442

543
If you opt for the MySQL configuration you will have to provide the database server yourself. You can also use MariaDB. Here are the minimum supported versions:
644

@@ -10,15 +48,7 @@ If you opt for the MySQL configuration you will have to provide the database ser
1048
It's easy to use another docker container for your database also and link it as part of the docker stack, so that's what the following examples
1149
are going to use.
1250

13-
::: warning
14-
15-
When using a `mariadb` database, the NPM configuration file should still use the `mysql` engine!
16-
17-
:::
18-
19-
## Running the App
20-
21-
Via `docker-compose`:
51+
Here is an example of what your `docker-compose.yml` will look like when using a MariaDB container:
2252

2353
```yml
2454
version: "3"
@@ -27,31 +57,26 @@ services:
2757
image: 'jc21/nginx-proxy-manager:latest'
2858
restart: unless-stopped
2959
ports:
30-
# Public HTTP Port:
31-
- '80:80'
32-
# Public HTTPS Port:
33-
- '443:443'
34-
# Admin Web Port:
35-
- '81:81'
60+
# These ports are in format <host-port>:<container-port>
61+
- '80:80' # Public HTTP Port
62+
- '443:443' # Public HTTPS Port
63+
- '81:81' # Admin Web Port
3664
# Add any other Stream port you want to expose
3765
# - '21:21' # FTP
3866
environment:
39-
# These are the settings to access your db
4067
DB_MYSQL_HOST: "db"
4168
DB_MYSQL_PORT: 3306
4269
DB_MYSQL_USER: "npm"
4370
DB_MYSQL_PASSWORD: "npm"
4471
DB_MYSQL_NAME: "npm"
45-
# If you would rather use Sqlite uncomment this
46-
# and remove all DB_MYSQL_* lines above
47-
# DB_SQLITE_FILE: "/data/database.sqlite"
4872
# Uncomment this if IPv6 is not enabled on your host
4973
# DISABLE_IPV6: 'true'
5074
volumes:
5175
- ./data:/data
5276
- ./letsencrypt:/etc/letsencrypt
5377
depends_on:
5478
- db
79+
5580
db:
5681
image: 'jc21/mariadb-aria:latest'
5782
restart: unless-stopped
@@ -64,13 +89,11 @@ services:
6489
- ./data/mysql:/var/lib/mysql
6590
```
6691
67-
_Please note, that `DB_MYSQL_*` environment variables will take precedent over `DB_SQLITE_*` variables. So if you keep the MySQL variables, you will not be able to use Sqlite._
92+
::: warning
6893
69-
Then:
94+
Please note, that `DB_MYSQL_*` environment variables will take precedent over `DB_SQLITE_*` variables. So if you keep the MySQL variables, you will not be able to use SQLite.
7095

71-
```bash
72-
docker-compose up -d
73-
```
96+
:::
7497

7598
## Running on Raspberry PI / ARM devices
7699

@@ -89,57 +112,7 @@ for a list of supported architectures and if you want one that doesn't exist,
89112
Also, if you don't know how to already, follow [this guide to install docker and docker-compose](https://manre-universe.net/how-to-run-docker-and-docker-compose-on-raspbian/)
90113
on Raspbian.
91114

92-
Via `docker-compose`:
93-
94-
```yml
95-
version: "3"
96-
services:
97-
app:
98-
image: 'jc21/nginx-proxy-manager:latest'
99-
restart: unless-stopped
100-
ports:
101-
# Public HTTP Port:
102-
- '80:80'
103-
# Public HTTPS Port:
104-
- '443:443'
105-
# Admin Web Port:
106-
- '81:81'
107-
environment:
108-
# These are the settings to access your db
109-
DB_MYSQL_HOST: "db"
110-
DB_MYSQL_PORT: 3306
111-
DB_MYSQL_USER: "changeuser"
112-
DB_MYSQL_PASSWORD: "changepass"
113-
DB_MYSQL_NAME: "npm"
114-
# If you would rather use Sqlite uncomment this
115-
# and remove all DB_MYSQL_* lines above
116-
# DB_SQLITE_FILE: "/data/database.sqlite"
117-
# Uncomment this if IPv6 is not enabled on your host
118-
# DISABLE_IPV6: 'true'
119-
volumes:
120-
- ./data/nginx-proxy-manager:/data
121-
- ./letsencrypt:/etc/letsencrypt
122-
depends_on:
123-
- db
124-
db:
125-
image: yobasystems/alpine-mariadb:latest
126-
restart: unless-stopped
127-
environment:
128-
MYSQL_ROOT_PASSWORD: "changeme"
129-
MYSQL_DATABASE: "npm"
130-
MYSQL_USER: "changeuser"
131-
MYSQL_PASSWORD: "changepass"
132-
volumes:
133-
- ./data/mariadb:/var/lib/mysql
134-
```
135-
136-
_Please note, that `DB_MYSQL_*` environment variables will take precedent over `DB_SQLITE_*` var>
137-
138-
Then:
139-
140-
```bash
141-
docker-compose up -d
142-
```
115+
Please note that the `jc21/mariadb-aria:latest` image might have some problems on some ARM devices, if you want a separate database container, use the `yobasystems/alpine-mariadb:latest` image.
143116

144117
## Initial Run
145118

0 commit comments

Comments
 (0)