Learning and Sharing
  • Home
  • Blog
  • Linux
  • macOS
  • Virtualization
    • VMware
    • VirtualBox
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
  • Home
  • Blog
  • Linux
  • macOS
  • Virtualization
    • VMware
    • VirtualBox
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
No Result
View All Result

Script: How to Install LEMP & WordPress on Ubuntu 20.04 LTS

October 26, 2022
in Blog, Linux
0
ADVERTISEMENT

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:

Note Note: Replace with the domain name you are using.
Note Note: To Save a file in Nano text editor, press , then press to confirm. To exit, press
#/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
................................................................
ADVERTISEMENT

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.

Bg2194
ADVERTISEMENT
5/5 - (1 vote)
Previous Post

How to Configure a Static IP for an Ubuntu Server VM in VMware

Next Post

How to Disable SELinux on CentOS 7 | Turn Off SELinux in CentOS 7

Related Posts

Running Hyper-V and VMware Workstation on The Same Machine

August 15, 2024

How to Uninstall All Autodesk Products At Once Silently

July 29, 2024
Ftr5

How to Uninstall the Autodesk Genuine Service on Windows

July 29, 2024
Ftr19

How to Fix Windows Cannot Read the ProductKey From the Unattend Answer File in VirtualBox

July 26, 2024
Ftr25

How to Update Windows Terminal in Windows 10/11

July 26, 2024

How to Disable The Beep Sound in WSL Terminal on Windows

July 26, 2024

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • How To Turn On uBlock Origin Extension in Chrome (2025)
  • Images Hidden Due To Mature Content Settings In CivitAI
  • Azure OpenAI vs Azure AI Hub, How to Choose the Right One for Your Needs

Categories

Stay in Touch

Discord Server

Join the Discord server with the site members for all questions and discussions.

Telegram Community

Jump in Telegram server. Ask questions and discuss everything with the site members.

Youtube Channel

Watch more videos, learning and sharing with Leo ❤❤❤. Sharing to be better.

Newsletter

Join the movement and receive our weekly Tech related newsletter. It’s Free.

General

Microsoft Windows

Microsoft Office

VMware

VirtualBox

Technology

PowerShell

Microsoft 365

Microsoft Teams

Email Servers

Copyright 2025 © All rights Reserved. Design by Leo with ❤

No Result
View All Result
  • Home
  • Linux
  • Intune
  • macOS
  • VMware
  • VirtualBox
  • Powershell
  • Windows 10
  • Windows 11
  • Microsoft 365
  • Microsoft Azure
  • Microsoft Office
  • Active Directory

No Result
View All Result
  • Home
  • Linux
  • Intune
  • macOS
  • VMware
  • VirtualBox
  • Powershell
  • Windows 10
  • Windows 11
  • Microsoft 365
  • Microsoft Azure
  • Microsoft Office
  • Active Directory