Install a Blog and Web Server with a Shell Script (Linux Automation — Part III)
This tutorial will focus on how to automate a local web server and Wordpress blog.
I was excited while figuring out how to do this because infrastructure automation is one of the things I’m passionate about IT.
In a future tutorial, I will show you how I automated live web sites with AWS.
Of course, we’ll start with the usual shebang and OS update.
Next, we’ll install the web server and the SQL database.
“httpd” is the installation package for the Apache web server. “mariadb” and “mariadb-server” are the installation packages for MySQL.
MariaDB is the open source version of MySQL (MySQL is owned by Oracle.)
Both “mariadb” and “mariadb-server” need to be installed; “mariadb” contains the base installation files for MariaDB, while “mariadb-server” contains the installation files that make MySQL fully functional.
Next, we configure firewall rules that permit HTTP and HTTPS traffic.
We start Apache and MariaDB. “Enable” runs Apache and MariaDB during future startups, “start” runs Apache and MariaDB immediately and “ — no-pager status” prints the status of Apache and MariaDB and exits out of the status page afterwards.
This step is probably the most crucial part of the script. We have to secure MariaDB.
Usually, when people set up MySQL, they run the command, mysql_secure_installation, on the command line. The first 6 SQL commands are equivalent to mysql_secure_installation.
The 6 SQL commands do the following:
- create a password for the SQL server’s root account
- remove anonymous user accounts
- delete root accounts that can login remotely
- delete the default database, “test”, which is accessible to anonymous users
- delete the contents of the “test” database
- reload the grant tables
The last 3 SQL commands do the following:
- create a SQL database for Wordpress
- create a user account
- grant the user account access to the Wordpress database
Next, we install the installation packages for PHP and restart the Apache server.
Next, we install Wordpress from wordpress.org to the “Downloads” directory.
Wordpress comes in a compressed file. We extract the contents of the compressed file, copy the contents to the “/var/www/html” (a sub-directory that was created when we installed Apache), copy the wp-config-sample.php and rename it wp-config.php (I’ll explain this in the next step) and change the ownership of the contents of “/var/www/html” subdirectory to the “apache”, a user and user-group that was created when we installed Apache. This allows Apache to run the Wordpress files.
Lastly, we configure Wordpress’ configuration file, wp-config.php. This will connect Wordpress to MariaDB by inserting the Wordpress database and user account that we created with the last 3 SQL commands we entered earlier.
If you followed this tutorial correctly, you should be able to enter “localhost” in your browser and be directed to the Wordpress admin page. Below is the script in its entirety:
#!/bin/bash
# Update the OS.
yum update -y# Install Apache and MySQL.
yum install -y httpd mariadb mariadb-server# Configure the firewall to allow HTTP & HTTPS traffic.
firewall_array=(‘ — add-service=http — zone=public — permanent’ ‘ — add-service=https — zone=public — permanent’ ‘ — reload’)
for i in ${apache_array[@]}
do
firewall-cmd $i
done# Start Apache.
apache_array=(‘enable’ ‘start’ ‘ — no-pager status’)
for i in ${apache_array[@]}
do
systemctl $i httpd
done# Start MySQL.
mysql_array=(‘enable’ ‘start’ ‘ — no-pager status’)
for i in ${mysql_array[@]}
do
systemctl $i mariadb
done# Secure MySQL and configure it for Wordpress.
echo ‘Enter a root password for MySQL:’
read -r -s “mysql_pass”echo ‘Enter a username for the Wordpress admin account:’
read -r “wp_name”echo ‘Enter a password for the Wordpress admin account:’
read -r -s “wp_pass”mysql <<_EOF_
UPDATE mysql.user SET Password=PASSWORD(‘${mysql_pass}’) WHERE User=’root’;
DELETE FROM mysql.user WHERE User=’’;
DELETE FROM mysql.user WHERE User=’root’ AND host NOT IN (‘localhost’, ‘127.0.0.1’, ‘::1’);
DROP DATABASE IF EXISTS test;
DELETE FROM mysql.db WHERE Db=’test’ OR Db=’test\\_%’;
FLUSH PRIVILEGES;
CREATE DATABASE wordpress;
CREATE USER ‘${wp_name}’@’localhost’ IDENTIFIED BY ‘${wp_pass}’;
GRANT ALL PRIVILEGES ON wordpress.* TO ‘${wp_name}’@’localhost’ IDENTIFIED BY ‘${wp_pass}’;
_EOF_# Install PHP.
yum install -y php php-mysql php-gd# Restart the Apache server.
systemctl restart httpd# Install Wordpress.
my_dir=”~/Downloads/”
wp_dir=”/var/www/html/”
wget https://wordpress.org/latest.tar.gz -P $my_dir
tar -xvf ${my_dir}latest.tar.gz -C $my_dir
cp -vR ${my_dir}wordpress/* $wp_dir
cp ${wp_dir}wp-config-sample.php ${wp_dir}wp-config.php
chown -R apache:apache ${wp_dir}*# Configure Wordpress with the MySQL authentication information.
sed -i -e “23s/database_name_here/wordpress/” ${wp_dir}wp-config.php
sed -i -e “26s/username_here/${wp_name}/” ${wp_dir}wp-config.php
sed -i -e “29s/password_here/${wp_pass}/” ${wp_dir}wp-config.php
View Part I of this tutorial series on DNS server automation.
View Part II of this tutorial series on DHCP server automation.