In my job like on other jobs, automatization is the main part of a job. Sometimes the same routine or process of development can spend valuable time and that is a huge loss. Installing WordPress over and over via console can be painful and needs a good concentration and knowledge. Therefore, I got idea to make a BASH script that would automatize the whole process.
My idea is to make a script which can download a fresh WordPress installation, make folder, unpack the installation files in this folder, prepare files, clean unnecessary files and set permissions. All of this needs to be done via command line and that can make a huge mess if you make a single mistake. That’s why I made this to be an all-arounder. I will first explain the whole process, part by part, and in the end I will leave you this BASH sript for automatization.
Before downloading, we need to prepare a directory in which a fresh WordPress installation will be located (on my example it’s /var/www/test directory). Open it afterwards:
> sudo mkdir -p /var/www/test
> cd /var/www/test
When we prepare that directory, the next step is to download the WordPress installation from https://wordpress.org/latest.zip:
> sudo wget https://wordpress.org/latest.zip
After this, with command
ls you can additionally check if latest.zip exists in the directory. The following step is to unzip your new installation files.
After this you will have a small problem. WordPress installation is now unpacked, but inside additional /wordpress directory inside /var/www/test and we need to move whole installation in the root folder of your project. Here is the command for that action:
> sudo cp -r /var/www/test/wordpress/* /var/www/test/
With this command you made a copy of whole WordPress installation in your project’s root folder /var/www/test. You may ask why I did not just move all files? The reason is simple. Copy operation performs a double check of its original destination and duplicated files on new. If some error happens, it can be easily seen. When you just move files, you may lose something because moving script just rewrites the new destination for those files.
You may configure (not necessary) permissions of your files. I configured permission on 777 in my DEV area to have full controll of files, and when I finish all changes I configure it to 775 or 664 before I place something “live”. You can set what you want here, and here is the example for it:
Now when we have all set in place, we need to clean the garbage. We need to remove /var/www/test/wordpress directory with all it’s data and also latests.zip.
> sudo rm -rf /var/www/wordpress
> sudo rm -f -r latests.zip
And we are ready to roll.
Like you’ve seen, the example above is a bit complicated and you need to do many things to get simple WordPress installation done. That’s why I offer one solution that is not perfect, but is really good for me.
Navigate to your home directory and in command line make a new file with command:
and copy inside it following code:
eval "mkdir -p "$dr" && cd "$dr" && wget "$wpInstall" && unzip "$zDir" && cp -r "$dr"/wordpress/* "$dr" && chmod -R 0777 "$dr" && rm -rf "$dr"/wordpress && rm -f -r "$zDir" && echo WordPress installation in directory "$dr" is done. "
… and save this file via CTRL+X. After that you need setup permission for your new BASH script:
> sudo chmod +x ./install-wp
And you are all done. Now you can easly install WordPress where you want to, by using the following command:
> sudo ./install-wp /var/www/test
And that’s it.
Now you need to make a new database in MySQL, navigate in browser to your new project like example http://127.0.0.1/test, finish your WordPress installation and you are up and running.
I have a future plan to incorporate in this script a database creation and automatic database connection, too. If you have some ideas or suggestions for improvements that you’d like to share with others, please contact me and we can publish that on our blog in the form of an additional article or update.
NOTE: This script is written on Debian Jessie 8.x and works also on other Linux versions but always double-check all before using it. You use this script at your own risk.
Automated WordPress Installation via BASH script on Linux
With BASH script you can automate one long proccess of WordPress installation via one simple command line and made all ready for work in less of 1 min.