Anthro Teaches You

How to write config files with bash script

Often you need to create configuration files inside a bash script such as when creating a bootstrap/setup script. It’s not very intuitive how you should should do it, and a lot of information found online is either unsafe or too complicated. Here is my preferred way of safely creating dynamic files using bash:

cat > /my/important/config.conf <<'EOF'
server {
    listen __PORT__;
    server_name __HOSTNAME__;
    
    location / {
        proxy_set_header Host $host;
    }
}
EOF

# If you need to also set permissions
chown user:group /my/important/config.conf
chmod 600 /my/important/config.conf

sed -i \
    -e "s|__PORT__|$PORT|g" \
    -e "s|__HOSTNAME__|$HOSTNAME|g" \
    /my/important/config.conf

The above sample combines Bash’s heredoc and sed to write a config file with placeholder values (optional) and then replace them with values from the script. Essentially, the heredoc begins with a limit string and will feed lines into a command until it reaches the limit string. In the example we feed input into cat which we redirect into a file.


How to install jschan on a VPS

In this article you will learn how to setup a VPS and install jschan an imageboard software written in JavaScript running on NodeJS. We will walk you through basic security, installing jschan, configuring our server, and how to use Cloudflare to protect our website from DDoS and malicious users.

This guide requires you to have basic familiarity with Linux, the terminal, access to a Virtual Private Server (VPS), and a domain registered with Cloudflare. How to obtain a VPS or domain is out of scope with this guide, and we will assume you have access to both.