Good Example
https://ayeshalshukri.co.uk/category/guides/bash-shell-script-for-deploying-your-web-app/
Contents
Have you ever struggled with a new WordPress site configuration? A new subdomain setup, generating the SSL certificate, creating an SSH account, GIT deploy keys setup, copying new WordPress files etc. All of this seems like a lot of work – work that can be automated.
It is crucial to have a test page (staging) when building a new WordPress website. In this tutorial, we will show how to use a single Bash script that will handle all of those tasks in a matter of seconds.
The Apache server uses FPM and has multiple PHP versions installed. Once the script is executed, we will see a wizard that includes questions about the domain name, PHP version and GIT repository address.
We assume that you have root access to the server and Apache installed. With small modifications, the script can also be used with NGINX.
Apache2 configuration
The first part is responsible for Apache server and PHP configuration. We’re creating the Home directory, a Linux user, applying correct permissions and owners of directories, creating an SSH connection, creating the virtualhost file, a directory with apache logs and a PHP FPM setup.
Creating the MYSQL database
The database will be created automatically with a random password generated. For this to work, your root MYSQL password needs to be hardcoded in the .sh file → replace YOUR_MYSQL_PASSWORD with your password.
Gitlab deploy keys
Our script deploys keys to the GITlab repo. This enables the ability to update the test server using one simple command: git pull. Make sure to replace YOUR_PRIVATE_TOKEN with a real value configured in your gitlab settings.
SSL certificate
We’re going to use certbot installed on Linux to create a free Let’s Encrypt SSL certificate. This will ensure that our site uses a secured https connection.
SSH account
We’re going to create an SSH account to have the ability to use the terminal to connect to a newly created website. The password is generated using the /dev/urandom function and will be displayed when the script finishes execution.
# passwd for unix username, save password
SSHPASS=`head /dev/urandom | tr -dc A-Za-z0-9 | head -c 10 ; echo ''`
echo -e "$SSHPASS\n$SSHPASS" | passwd $DOMAIN
Git init
At this point we have a working test page with https, but without files. Let’s start by adding GIT initialization. We will fetch files from the Gitlab repository (for example files from /wp-content/themes/ and /wp-content/plugins/). Git will not ask for a password because deploy keys are already configured and the process will be done without user interaction.
WP CLI
To add WordPress core files and create wp-config.php, we use WP CLI, a command-line interface for WordPress. It allows to fetch the newest WordPress files from the original WordPress server. At the end, we use the touch command to create WP .htaccess with default content.
Htpassword
The test page will be protected by an htpasswd file. To access the website, you need to fill in the login details: testpage_user and testpage_password.
Displaying summary
One last step, our bash script will display all the details about the just created test page. We can copy and save them somewhere in the Notes for future use.
Bash script to set up the WordPress test page
The bash script starts with wizard questions, then it will start the setup. Everything takes 45-60 seconds. At the end, the test page is ready to use. To change theme files on the test page, we can add changes to the GITlab repository and then connect via SSH and fetch updates from GIT repo using the following command:
Here is a full setup-new-testpage.sh bash script file.
FPM config
PHP-FPM (FastCGI Process Manager) is a server tool to speed up the performance of the site. It’s faster than traditional CGI and can handle heavy load simultaneously. Here is the location of our FPM template: /root/new-testpage/fpm.conf.template
Apache virtualhost
The term Virtual Host (vhost) refers to the practice of running more than one website (such as company1.example.com and company2.example.com) on one server. We’re using the “name-based” vhost, meaning that multiple names are running on one IP address. We’re using a predefined template. Here is the source code of /root/new-testpage/apache.virtualhost.dev.template :
Creating a new test site
To use this script, just change 3 strings:
To change WordPress access to wp-admin panel, those values should be replaced in bash script:
The website will be set up at: https://subdomain.YOUR_COMPANY.dev url address.
To execute the bash script, we need to add permissions for execution, it’s a one-time operation:
chmod u+x setup-new-testpage.sh
Now, we can run the wizard for a new test page setup:
./setup-new-testpage.sh
Bash wizard
After script execution, we will be asked 6 questions.
All tasks for staging site setup take 45-60 seconds. After finishing all of its “magic”, the script will display a success message:
Alternatives
Not feeling strong in server configuration and running your own test pages? There are alternatives. The most popular one is using hosting companies. New pages can be created by clicking a button in the hosting panel. Some of them offer GIT repositories and an easy way of cloning a website to the staging environment – WP ENGINE being one of them.
There are tradeoffs of using Hosting companies. It’s more pricey than running your own server. However, it can be convenient for someone who is not a server expert. It’s worth to mention that hosting companies also offer additional services like WAF Firewalls and regular server updates, which will help with the security of your website.
That’s it for today’s tutorial. Make sure to follow us for other useful tips and guidelines, and don’t forget to subscribe to our newsletter.
Leave A Comment?