Skip to content

Commit

Permalink
Add node server. Fix scripts. Fix sample folder
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewPattell committed Mar 17, 2020
1 parent dd61ad8 commit c553afa
Show file tree
Hide file tree
Showing 16 changed files with 260 additions and 87 deletions.
88 changes: 59 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,53 +1,81 @@
Universal docker server
===========================
Nginx (1.17.8), PHP (7.4), MySql (5.7), Redis (5.0.7)
Nginx (1.17.8), PHP (7.4) / Node (13.10), MySql (5.7), Redis (5.0.7)

Installation
------------

The preferred way to install this extension is through [composer](http:https://getcomposer.org/download/).
The preferred way to install this extension.

Either run

```bash
php composer.phar require --prefer-dist matthew-p/docker-server "^4.11.0"
```

or add

```
"matthew-p/docker-server": "^4.11.0"
```

to the require section of your `composer.json` file.
- for **composer**
```bash
php composer.phar require --prefer-dist matthew-p/docker-server
```

or add

```
"matthew-p/docker-server": "^4.11.0"
```

to the require section of your `composer.json` file.
- for **npm**
```bash
npm i --save @matthew-p/docker-server@latest
```
or add

```
"@matthew-p/docker-server": "^4.11.0"
```
to the dependencies section of your `package.json` file.

## After install package:

1. Add or update section **scripts** in **composer.json**:

```json5
{
"scripts": {
"server": "vendor/bin/site-start.sh --env-file=docker/.env.local",
"server-run": "vendor/bin/site-run.sh --env-file=docker/.env.local",
"server-exec": "vendor/bin/site-exec.sh --env-file=docker/.env.local",
1. Add or update section **scripts** in **composer.json** or **package.json**:

// (optional)
"server-prod": "vendor/bin/site-aws.sh --env-file=docker/.env.prod",
// (optional)
"server-deploy-dev": "vendor/bin/site-deploy.sh --env-file=docker/.env.dev"
- for **composer.json**
```json5
{
"scripts": {
"server": "vendor/bin/site-start.sh --env-file=docker/.env.local",
"server-run": "vendor/bin/site-run.sh --env-file=docker/.env.local",
"server-exec": "vendor/bin/site-exec.sh --env-file=docker/.env.local",

// (optional)
"server-prod": "vendor/bin/site-aws.sh --env-file=docker/.env.prod",
// (optional)
"server-deploy-dev": "vendor/bin/site-deploy.sh --env-file=docker/.env.dev"
}
}
}
```
```
- for **package.json**
```json5
{
"scripts": {
"server": "docker-server-start --env-file=docker/.env.local",
"server-run": "docker-server-run --env-file=docker/.env.local",
"server-exec": "docker-server-exec --env-file=docker/.env.local",

// (optional)
"server-prod": "docker-server-aws --env-file=docker/.env.prod",
// (optional)
"server-deploy-dev": "docker-server-deploy --env-file=docker/.env.dev"
}
}
```

where **"docker/.env.local"** relative path to your local env config _(will be created in next step)_.

1. Run: ```composer server init```. This will create a **docker** folder in your project root directory.
1. Run: ```composer server init``` or ```npm run server init```. This will create a **docker** folder in your project root directory.

1. Change **root-path** in _docker/nginx/conf-dynamic.d/sample.conf_

1. See [supported os](#supported-os) and config **docker/.env.local** according to your operating system

1. Run server: ```composer server up ```
1. Run server: ```composer server up ``` or ```npm run server up```

## Supported OS
- [Linux](docs/LINUX.md)
Expand All @@ -69,6 +97,8 @@ to the require section of your `composer.json` file.
- matthewpatell/universal-docker-server:4.0-jre (with java)
- PHP-FPM:
- matthewpatell/universal-docker-server-php-fpm:4.0
- NODE:
- node:13.10

**FEATURES**
---
Expand Down
51 changes: 43 additions & 8 deletions bin/site-start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,38 @@ VENDOR_DIR=$(getVendorPath "${BASH_SOURCE[0]}")
. "${VENDOR_DIR}/helpers/compile-env.sh"

ACTION=$1
DETACHED_MODE=$DEFAULT_DETACHED_MODE
DETACHED_MODE=${DEFAULT_DETACHED_MODE}

if [ "$ACTION" = "compile" ]; then
if [[ "$ACTION" = "compile" ]]; then
echo "Env file created success"
exit 0
fi

if [[ "${ACTION}" != "up" ]] && [ "$DETACHED_MODE" = "-d" ]; then
if [[ "${ACTION}" != "up" ]] && [[ "$DETACHED_MODE" = "-d" ]]; then
DETACHED_MODE=""
fi

COMMAND=(docker-compose $SERVICES $ACTION $DETACHED_MODE $OTHER_PARAMS)

if [ "$ACTION" = "restart" ]; then
if [[ "$ACTION" = "restart" ]]; then
# Stop
"${COMMAND[@]/restart/down}"
# Recompile/reexport env
. "${VENDOR_DIR}/helpers/compile-env.sh" "up"
# Run
"${COMMAND[@]/restart/up}" $DEFAULT_DETACHED_MODE
elif [ "$ACTION" = "init" ]; then
elif [[ "$ACTION" = "init" ]]; then

TARGET_DIR="$VENDOR_PARENT_DIR/docker"
IS_NODE=0

if [ ! -d "$TARGET_DIR" ]; then
if [[ ! -z $(env | grep "npm_package*") ]]; then
IS_NODE=1
fi

if [[ ! -d "$TARGET_DIR" ]]; then
cp -r "$VENDOR_DIR/sample" "$TARGET_DIR/"
mv "$TARGET_DIR/gitignore" "$TARGET_DIR/.gitignore"
mv "$TARGET_DIR/.env-sample" "$TARGET_DIR/.env.local"
mv "$TARGET_DIR/.env-sample-prod" "$TARGET_DIR/.env.prod"
mv "$TARGET_DIR/docker-compose.local-sample.yml" "$TARGET_DIR/docker-compose.local.yml"
Expand All @@ -55,8 +61,37 @@ elif [ "$ACTION" = "init" ]; then
sed -i "" "s/change_this_string/$RANDKEY/g" "$TARGET_DIR/$file"
done

# Configure NODE env
if [[ "$IS_NODE" == 1 ]]; then
# Fix nginx template for node
sed -i "" "s/\/var\/www\/html\/frontend\/web/\/var\/www\/html/g" "$TARGET_DIR/nginx/conf-dynamic.d/domain.conf"
sed -i "" "s/templates\/domain.conf/templates\/node.conf/g" "$TARGET_DIR/nginx/conf-dynamic.d/domain.conf"

# Fix env services
sed -i "" "s/\${SERVICES}/\${SERVICES_NODE}/g" "$TARGET_DIR/.env.local"

# Remove prod env (must be created manualy)
rm "$TARGET_DIR/.env.prod"
rm "$TARGET_DIR/docker-compose.static-network.yml"
fi

if [[ -f "$VENDOR_DIR/docker/.env" ]]; then
rm "$VENDOR_DIR/docker/.env"
fi

# Configure macos
if [[ "$OSTYPE" == "darwin"* ]]; then
sed -i "" "s/# HOST_NGINX_CONF_DIR/HOST_NGINX_CONF_DIR/g" "$TARGET_DIR/.env.local"
sed -i "" "s/# HOST_NGINX_RESTART_COMMAND/HOST_NGINX_RESTART_COMMAND/g" "$TARGET_DIR/.env.local"
sed -i "" "s/# HOST_ETC_HOST_IP/HOST_ETC_HOST_IP/g" "$TARGET_DIR/.env.local"
else
sed -i "" "s/# HOST_NGINX_CONF_DIR//g" "$TARGET_DIR/.env.local"
sed -i "" "s/# HOST_NGINX_RESTART_COMMAND//g" "$TARGET_DIR/.env.local"
sed -i "" "s/# HOST_ETC_HOST_IP//g" "$TARGET_DIR/.env.local"
fi

echo "Server init success."
echo "Change root-path in: $TARGET_DIR/nginx/conf-dynamic.d/sample.conf"
echo "Change root-path in: $TARGET_DIR/nginx/conf-dynamic.d/domain.conf"
else
echo "$TARGET_DIR folder already exists"
fi
Expand All @@ -66,7 +101,7 @@ else
"${COMMAND[@]}"
fi

if [ "$ACTION" = "up" ] || [ "$ACTION" = "down" ] || [ "$ACTION" = "restart" ]; then
if [[ "$ACTION" = "up" ]] || [[ "$ACTION" = "down" ]] || [[ "$ACTION" = "restart" ]]; then
# Auto update hosts file for host
. "${VENDOR_DIR}/helpers/update-hosts.sh" "$ACTION"

Expand Down
7 changes: 4 additions & 3 deletions docker/.env-default
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ DEFAULT_DETACHED_MODE=-d
#PROJECT_ENV_PATH_FORCE=/custom/path
# use !! for vendor path
SERVICES="-f !!docker-compose.nginx.yml -f !!docker-compose.php-fpm.yml -f !!docker-compose.redis.yml -f !!docker-compose.mysql.yml -f !!docker-compose.dev.yml"
SERVICES_NODE="-f !!docker-compose.nginx.yml -f !!docker-compose.node.yml"

# SERVER
SERVER_HTTP_PORT=80
Expand Down Expand Up @@ -85,15 +86,15 @@ NGINX_DEFAULT_HOST=yes
# Set yes for keep created conf file
HOST_NGINX_KEEP_CONF=no
HOST_NGINX_TEMPLATE_PATH=${VENDOR_DIR}/conf/nginx-proxy.conf
HOST_NGINX_CONF_DIR=/usr/local/etc/nginx/servers
HOST_NGINX_RESTART_COMMAND="sudo brew services restart nginx"
HOST_NGINX_CONF_DIR=/etc/nginx/sites-enabled
HOST_NGINX_RESTART_COMMAND="service nginx restart"

# HOST AUTO UPDATE /etc/hosts
HOST_ETC_HOSTS_UPDATE=no
HOST_ETC_HOST_PATH=/etc/hosts
# Set yes for keep generated hosts
HOST_ETC_KEEP_CONF=no
# Keep empty for get nginx container ip
# Keep empty for get nginx container ip (linux)
HOST_ETC_HOST_IP=

# FTP
Expand Down
71 changes: 71 additions & 0 deletions docker/nginx/templates/node-server.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Variable automaticaly replaced:
# SSL_INCLUDE, COMMON_DOMAIN, PARSED_DOMAINS, DOMAIN_1LVL, DOMAIN_2LVL, CERTIFICATE_DOMAIN

# allow domains:
#
# <domains-include>DOMAIN_COMMON</domains-include>
#
# parsed domains: $PARSED_DOMAINS

# root path:
#
# <root-path>/var/www/html</root-path>

# change certificate domain:
#
# <certificate-domain>$CERTIFICATE_DOMAIN</certificate-domain>

# Custom snippets:
#
# <custom-snippets></custom-snippets>

server {
charset utf-8;
client_max_body_size 128M;

listen 80$DEFAULT;

server_name $COMMON_DOMAIN;

root $ROOT_PATH;
index index.html;

# if ssl certificate exist for domain, here will be included ssl directives
$SSL_INCLUDE

include snippets/letsencrypt-acme-challenge.conf;
include snippets/protection.conf;
include snippets/rewrite.conf;
include snippets/blacklist_ip.conf;

$CUSTOM_SNIPPETS

location = /favicon.ico {
log_not_found off;
access_log off;
}

location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}

location / {
limit_conn conn_limit_per_ip 10;
limit_req zone=req_limit_per_ip burst=10 nodelay;
proxy_buffers 4 32k;
proxy_buffer_size 32k;
proxy_busy_buffers_size 32k;

proxy_pass http:https://server:3000;
proxy_redirect off;

proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
1 change: 0 additions & 1 deletion docker/nginx/templates/node.conf
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ server {
include snippets/letsencrypt-acme-challenge.conf;
include snippets/protection.conf;
include snippets/rewrite.conf;
include snippets/static.conf;
include snippets/blacklist_ip.conf;

$CUSTOM_SNIPPETS
Expand Down
5 changes: 3 additions & 2 deletions docs/ENVIRONMENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Property | Values | Description
---------|--------|------------
`NGINX_REPOSITORY`| `(string)` `Default: matthewpatell/universal-docker-nginx:3.7` | Nginx docker image
`SERVER_REPOSITORY`| `(string)` `Default: matthewpatell/universal-docker-server:4.0-dev` | Either server (or server dev) image in non-scalable configuration, or `matthewpatell/universal-docker-server-php-fpm` for scaling `nginx`+`php-fpm`.
`SERVER_NODE_REPOSITORY`| `(string)` `Default: node:13.10` | Either server image for scaling `nginx`+`node`.
`TERMINAL_REPOSITORY`| `(string)` `Default: none` | Static non-scalable docker image for ssh connection, cron, queue listener and etc. Use image: `matthewpatell/universal-docker-server`.

## <a id="server-envs"></a>Server env (service: server)
Expand Down Expand Up @@ -133,8 +134,8 @@ Property | Values | Description
`HOST_NGINX_PROXIES`| `yes`/`no` `Default: no` | Create nginx proxy config file automatically (proxy to docker container nginx)
`HOST_NGINX_KEEP_CONF`| `yes`/`no` `Default: no` | Set `yes` for keep created nginx proxy conf file after `server down`
`HOST_NGINX_TEMPLATE_PATH`| `(string)` `Default: ${VENDOR_DIR}/conf/nginx-proxy.conf` | Template nginx proxy conf
`HOST_NGINX_CONF_DIR`| `(string)` `Default: /usr/local/etc/nginx/servers` | Nginx configs dir on host machine
`HOST_NGINX_RESTART_COMMAND`| `(string)` `Default: "sudo brew services restart nginx"` | Restart nginx command on host machine
`HOST_NGINX_CONF_DIR`| `(string)` `Default: /etc/nginx/sites-enabled` | Nginx configs dir on host machine
`HOST_NGINX_RESTART_COMMAND`| `(string)` `Default: "service nginx restart"` | Restart nginx command on host machine
`NGINX_DEFAULT_HOST` | `yes`/`no` `Default: no` | Enable/disable default nginx host

## <a id="auto-update-hosts-envs"></a>Auto update hosts env (host machine)
Expand Down
Loading

0 comments on commit c553afa

Please sign in to comment.