我正在编写一个脚本来自动为我自己的web服务器创建Apache和PHP的配置文件.我不想使用像CPanel或ISPConfig这样的任何GUI.
我有一些Apache和PHP配置文件的模板.Bash脚本需要读取模板,进行变量替换并将解析后的模板输出到某个文件夹中.最好的方法是什么?我可以想到几种方法.哪一个是最好的还是有更好的方法可以做到这一点?我想在纯Bash中做到这一点(例如在PHP中很容易)
template.txt:
the number is ${i}
the word is ${word}
Run Code Online (Sandbox Code Playgroud)
script.sh:
#!/bin/sh
#set variables
i=1
word="dog"
#read in template one line at the time, and replace variables
#(more natural (and efficient) way, thanks to Jonathan Leffler)
while read line
do
eval echo "$line"
done < "./template.txt"
Run Code Online (Sandbox Code Playgroud)
顺便说一句,如何在此处将输出重定向到外部文件?如果变量包含引号,我是否需要逃避某些事情?
2)使用cat&sed替换每个变量的值:
给出template.txt:
The number is ${i}
The word is ${word}
Run Code Online (Sandbox Code Playgroud)
命令:
cat template.txt | sed -e "s/\${i}/1/" | sed -e "s/\${word}/dog/"
Run Code Online (Sandbox Code Playgroud)
对我来说似乎不好,因为需要逃避许多不同的符号,并且对于许多变量,这条线太长了.
你能想到其他一些优雅而安全的解决方案吗?
这是我关于这个特定问题的第二篇文章。我已经删除了那个问题,因为我找到了一种更好的方法来解释我到底想做什么。
本质上,我想将命令行参数传递给docker-compose up
我的 Vue.js Web 应用程序并将它们设置为环境变量。目标是能够更改环境变量而无需每次都重建容器。
我遇到了几个问题。这是我的 docker 文件:
用于 Vue.js 应用程序的 Dockerfile。
FROM node:latest as build-stage
WORKDIR /app
# Environment variable.
ENV VUE_APP_FOO=FOO
COPY package*.json ./
RUN npm install
COPY ./ .
RUN npm run build
FROM nginx as production-stage
RUN mkdir /app
COPY --from=build-stage /app/dist /app
COPY nginx.conf /etc/nginx/nginx.conf
Run Code Online (Sandbox Code Playgroud)
VUE_APP_FOO
通过 Node 的process.env
objected存储和访问,并且似乎在构建时传入。
还有我的 docker-compose.yml:
version: '3.5'
services:
ms-sql-server:
image: mcr.microsoft.com/mssql/server:2017-latest-ubuntu
ports:
- "1430:1433"
api:
image: # omitted (pulled from url)
restart: always
depends_on: …
Run Code Online (Sandbox Code Playgroud) 我正在尝试在通过 docker-compose 配置的 docker 容器中启动 NGINX 服务器。然而,问题是我想替换 http 部分内的环境变量,特别是在“上游”块内。
让它工作会很棒,因为我还有其他几个容器都是通过环境变量配置的,并且我有大约 5 个环境需要在任何给定时间运行。我曾尝试使用“envsubst”(如官方 NGINX 文档所建议的)、perl_set 和 set_by_lua,但是它们似乎都不起作用。
下面是 NGINX 配置,因为它是在我最近的试用之后
user nginx;
worker_processes 1;
env NGINXPROXY;
load_module modules/ngx_http_perl_module.so;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
perl_set $nginxproxy 'sub { return $ENV{"NGINXPROXY"}; }';
upstream api-upstream {
server ${nginxproxy};
}
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile off;
#tcp_nopush on;
keepalive_timeout 65;
#gzip …
Run Code Online (Sandbox Code Playgroud) 我之前在 AWS Linux AMI 上有过这个工作,但在 AWS Linux 2 上没有运气。
我需要在 EB 应用程序部署期间从 Nginx 配置文件访问我的环境属性。它是一个单实例节点服务器。
我使用 AWS Linux AMI 是这样做的,并且没有问题:
.ebextensions/00_options.config
option_settings:
aws:elasticbeanstalk:application:environment:
DOMAIN: socket.example.com
MASTER_DOMAIN: https://example.com
etc..
Run Code Online (Sandbox Code Playgroud)
.ebextensions/10_proxy.config
... some configs ...
files:
/etc/nginx/conf.d/proxy.conf:
mode: "000644"
owner: root
group: root
content: |
upstream nodejs {
server 127.0.0.1:8081;
keepalive 256;
}
map $http_origin $cors_header {
hostnames;
default "";
`{"Fn::GetOptionSetting": {"Namespace": "aws:elasticbeanstalk:application:environment", "OptionName": "MASTER_DOMAIN"}}` "$http_origin";
}
server {
listen 80;
listen 8080;
server_name `{"Fn::GetOptionSetting": {"Namespace": "aws:elasticbeanstalk:application:environment", "OptionName": "DOMAIN"}}`;
location ~ /.well-known { …
Run Code Online (Sandbox Code Playgroud) nginx environment-variables amazon-web-services amazon-elastic-beanstalk amazon-linux-2
我尝试重定向到代理服务器 nginx。
location /phpmyadmin {
proxy_http_version 1.1;
proxy_pass https://${PMA}:5000/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
Run Code Online (Sandbox Code Playgroud)
但我收到错误:
nginx: [emerg] invalid number of arguments in "proxy_set_header" directive in /etc/nginx/nginx.conf:26
Run Code Online (Sandbox Code Playgroud)
此清单中检查错误的完整代码,因为我真的找不到某些错误(${env} = 脚本中的正确更改
user root;
worker_processes auto;
pcre_jit on;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
keepalive_timeout 3000;
sendfile on;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
server {
listen 443 ssl;
listen [::]:443 ssl;
ssl_certificate /etc/ssl/nginx.crt;
ssl_certificate_key /etc/ssl/nginx.key;
root /home;
index default.html /default.html;
location /phpmyadmin {
proxy_http_version 1.1;
proxy_pass https://${PMA}:5000/; …
Run Code Online (Sandbox Code Playgroud) reverse-proxy nginx docker docker-compose nginx-reverse-proxy