Iâve created or rather copied from a number of sources the necessary pieces in order to create my own bash script that will install WP via ssh. Iâm not a coder at all, so many things will go over my head. But my patience and desire to learn will compensate.
Iâve created a bash script to download wordpress, create a new database (with correct permissions) then open the wp-config-sample.php file, rename it to wp-config.php file then open it up using nano. This is where Iâm stuck. Iâd like to use the details from the newly created database and have those details automatically inserted into the correct places within the wp-config file. Then Iâd like to visit this url (https://api.wordpress.org/secret-key/1.1/salt/) and take those salt keys and also save them in the same wp-config.php file then save and quit.
Please help!
I don’t think
nano
is going to be scriptable in the manner you need.You need to look into
Templates
. The general idea is to put placeholders like%DB_NAME%
,%DB_USER%
, inside a template file likewp-config-template.php
. Then you can use something likesed
to search and replace these placeholders with real values, to generate the finalwp-config.php
.However doing this in
sed
will get messy pretty quickly. You are better of using a scripting language like perl or python when you start doing templating. They will do the heavy lifting for you.Finally, not to discourage your efforts. But building a server like you are is tricky, because there are number of things that could go wrong. Also you need to be idempotent, ie:- the ability to rerun your entire set of scripts, and only applying changes. So if you decide to add/remove packages the rest of the system should remain untouched.
If you are looking for a good guideline containing some the best tools to do this, you should check out the Genesis-WordPress project. It uses Ansible for provisioning and Capistrano for deployment.
Good Luck!
If you want a disgusting, but functional example on how to change wp-config.php from environmental variables in a bash script, check out the “official” WordPress scripts for creating a Docker image.