Table of Contents
In some cases, you want to automate the installation of the Nginx web server, MySQL Database, PHP, and WordPress.
I have created a script that comprises all commands we need to install LEMP on Ubuntu 20.04 LTS Focal Fossa and over that WordPress.
This is really handy if you want to set up and run a WordPress blog or website on your Ubuntu Linux server instantly without punching each command one by one.
Install LEMP and WordPress on Ubuntu 20.04 LTS
What you have to do is open the command terminal or if you are using a VPS Hosting or a Cloud server then you are already there.
1. Install the nano editor then create a script file.
sudo apt -y update
sudo apt -y install nano
nano installer.sh
2. Now, copy then paste all the below commands into the created file:
#/bin/sh
#Creating Variables
domain_name="bonguides.me"
install_dir="/usr/share/nginx/$domain_name"
#Creating Random WP Database Credenitals
db_name="wp`date +%s`"
db_user=$db_name
db_password=`date |md5sum |cut -c '1-12'`
sleep 5
mysqlrootpass=`date |md5sum |cut -c '1-12'`
sleep 5
#### Install Packages for nginx and mysql
apt -y update && apt -y upgrade
apt -y install nginx mariadb-server mariadb-client
#### Open firewall port for http/https
sudo iptables -I INPUT -p tcp --dport 80 -j ACCEPT
sudo iptables -I INPUT -p tcp --dport 443 -j ACCEPT
sudo ufw allow http && sudo ufw allow https
#### Start, enable on boot and set root password
systemctl restart mariadb.service && systemctl enable mariadb.service
mysql -u root -e "UPDATE user SET Password=PASSWORD($mysqlrootpass) WHERE user='root'";
mysql -u root -e "CREATE USER $db_user@localhost IDENTIFIED BY '$db_password'";
mysql -u root -e "create database $db_name";
mysql -u root -e "GRANT ALL PRIVILEGES ON $db_name.* TO $db_user@localhost";
mysql -u root -e "FLUSH PRIVILEGES";
####Install PHP and increase file size
sudo apt install -y php7.4 php7.4-fpm php7.4-mysql php-common php7.4-cli
sudo apt install -y php7.4-json php7.4-opcache php7.4-readline php7.4-mbstring
sudo apt install -y php7.4-xml php7.4-gd php7.4-curl php7.4-common
sudo sed -i 's/upload_max_filesize = 2M/upload_max_filesize = 64M/g' /etc/php/7.4/fpm/php.ini
sudo sed -i 's/post_max_size = 8M/post_max_size = 64M/g' /etc/php/7.4/fpm/php.ini
systemctl restart php7.4-fpm && systemctl enable php7.4-fpm
####Download and extract latest WordPress Package
wget -N "https://wordpress.org/latest.tar.gz" -P /tmp
tar xf /tmp/latest.tar.gz -C /tmp
mkdir $install_dir
sudo mv /tmp/wordpress/* $install_dir
sudo chown www-data:www-data $install_dir -R
sudo rm /etc/nginx/sites-enabled/default
####Create a virtual host
cat << EOF >> /etc/nginx/conf.d/$domain_name.conf
# BEGIN
server {
listen 80;
listen [::]:80;
server_name www.$domain_name $domain_name;
root /usr/share/nginx/$domain_name/;
index index.php index.html index.htm index.nginx-debian.html;
location / {
try_files \$uri \$uri/ /index.php;
}
location ~ ^/wp-json/ {
rewrite ^/wp-json/(.*?)\$ /?rest_route=/\$1 last;
}
location ~* /wp-sitemap.*\.xml {
try_files \$uri \$uri/ /index.php\$is_args\$args;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
client_max_body_size 20M;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php\$ {
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
include fastcgi_params;
include snippets/fastcgi-php.conf;
# Add headers to serve security related headers
add_header X-Content-Type-Options nosniff;
add_header X-XSS-Protection "1; mode=block";
add_header X-Permitted-Cross-Domain-Policies none;
add_header X-Frame-Options "SAMEORIGIN";
}
#enable gzip compression
gzip on;
gzip_vary on;
gzip_min_length 1000;
gzip_comp_level 5;
gzip_types application/json text/css application/x-javascript application/javascript image/svg+xml;
gzip_proxied any;
# A long browser cache lifetime can speed up repeat visits to your page
location ~* \.(jpg|jpeg|gif|png|webp|svg|woff|woff2|ttf|css|js|ico|xml)$ {
access_log off;
log_not_found off;
expires 360d;
}
# disable access to hidden files
location ~ /\.ht {
access_log off;
log_not_found off;
deny all;
}
}
# END
EOF
#### Create WP-config and set DB credentials
/bin/mv $install_dir/wp-config-sample.php $install_dir/wp-config.php
/bin/sed -i "s/database_name_here/$db_name/g" $install_dir/wp-config.php
/bin/sed -i "s/username_here/$db_user/g" $install_dir/wp-config.php
/bin/sed -i "s/password_here/$db_password/g" $install_dir/wp-config.php
#### Restart all services
chown www-data: $install_dir -R
systemctl restart nginx && systemctl restart php7.4-fpm && systemctl restart mariadb.service
######Display generated passwords to log file.
echo "................................................................"
echo "..... The installation was successfull ....."
echo "................................................................"
echo "Database Name: " $db_name
echo "Database User: " $db_user
echo "Database Password: " $db_password
echo "Mysql root password: " $mysqlrootpass
echo "................................................................"
echo "................................................................"
echo "..... Your site information ....."
echo "................................................................"
echo "Your site: http://$domain_name"
echo "WordPress admin: http://$domain_name/wp-admin"
echo "Install localtion: $install_dir"
echo "................................................................"
3. Mark the file as executable, then run the script.
sudo chmod +x installer.sh
./installer.sh
4. Wait for a few minutes depending upon your internet connection, it will set up Nginx, MariaDB, PHP 7.4, and WordPress.
5. Once the script gets completed it will also show the created Database, username, and MySQL root password. Note that down somewhere.
................................................................
..... The installation was successfull .....
................................................................
Database Name: wp1666764661
Database User: wp1666764661
Database Password: fcafd812727d
Mysql root password: c6d871fd54b1
................................................................
................................................................
..... Your site information .....
................................................................
Your site: http://bonguides.me
WordPress admin: http://bonguides.me/wp-admin
Install localtion: /usr/share/nginx/bonguides.me
................................................................
6. Open your browser visit your site http://domain.com or http://server_ip_address. It will show the process to set up the WordPress website including username and password for the same.