Script to install LAMP & WordPress on Ubuntu 20.04 LTS server quickly with one command

Do you want to automate the installation of the Apache web server, MySQL Database, PHP, and WordPress? Then here is the tutorial on that.

I have created a script that comprises all commands we need to install LAMP 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 LAMP & WordPress using Script on Ubuntu 20.04 LTS

What you have to do is open the command terminal or if you are using VPS Hosting or Cloud server then you are already there.

Switch to root user:

sudo su -

Create a script file:

apt install nano -y
nano word.sh

Now, copy-paste all the commands, given below in the created file:

#/bin/sh

install_dir="/var/www/html"
#Creating Random WP Database Credenitals
db_name="wp`date +%s`"
db_user=$db_name
db_password=`date |md5sum |cut -c '1-12'`
sleep 1
mysqlrootpass=`date |md5sum |cut -c '1-12'`
sleep 1

#### Install Packages for https and mysql
apt -y update 
apt -y upgrade
apt -y install apache2
apt -y install mysql-server


#### Start http
rm /var/www/html/index.html
systemctl enable apache2
systemctl start apache2

#### Start mysql and set root password

systemctl enable mysql
systemctl start mysql

/usr/bin/mysql -e "USE mysql;"
/usr/bin/mysql -e "UPDATE user SET Password=PASSWORD($mysqlrootpass) WHERE user='root';"
/usr/bin/mysql -e "FLUSH PRIVILEGES;"
touch /root/.my.cnf
chmod 640 /root/.my.cnf
echo "[client]">>/root/.my.cnf
echo "user=root">>/root/.my.cnf
echo "password="$mysqlrootpass>>/root/.my.cnf
####Install PHP
apt -y install php php-bz2 php-mysqli php-curl php-gd php-intl php-common php-mbstring php-xml

sed -i '0,/AllowOverride\ None/! {0,/AllowOverride\ None/ s/AllowOverride\ None/AllowOverride\ All/}' /etc/apache2/apache2.conf #Allow htaccess usage

systemctl restart apache2

####Download and extract latest WordPress Package
if test -f /tmp/latest.tar.gz
then
echo "WP is already downloaded."
else
echo "Downloading WordPress"
cd /tmp/ && wget "http://wordpress.org/latest.tar.gz";
fi

/bin/tar -C $install_dir -zxf /tmp/latest.tar.gz --strip-components=1
chown www-data: $install_dir -R

#### 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

cat << EOF >> $install_dir/wp-config.php
define('FS_METHOD', 'direct');
EOF

cat << EOF >> $install_dir/.htaccess
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ – [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
EOF

chown www-data: $install_dir -R

##### Set WP Salts
grep -A50 'table_prefix' $install_dir/wp-config.php > /tmp/wp-tmp-config
/bin/sed -i '/**#@/,/$p/d' $install_dir/wp-config.php
/usr/bin/lynx --dump -width 200 https://api.wordpress.org/secret-key/1.1/salt/ >> $install_dir/wp-config.php
/bin/cat /tmp/wp-tmp-config >> $install_dir/wp-config.php && rm /tmp/wp-tmp-config -f
/usr/bin/mysql -u root -e "CREATE DATABASE $db_name"
/usr/bin/mysql -u root -e "CREATE USER '$db_name'@'localhost' IDENTIFIED WITH mysql_native_password BY '$db_password';"
/usr/bin/mysql -u root -e "GRANT ALL PRIVILEGES ON $db_name.* TO '$db_user'@'localhost';"
 
######Display generated passwords to log file.
echo "Database Name: " $db_name
echo "Database User: " $db_user
echo "Database Password: " $db_password
echo "Mysql root password: " $mysqlrootpass
 

To save the script file press Crtl+X and then type Y and hit the Enter button.

Now run the Script:

sh word.sh

Wait for a few minutes depending upon your internet connection, it will set up Apache, MySQL, PHP, and WordPress. Once the script gets completed it will also show the created Database, username, and MySQL root password.  Note that down somewhere.

Open your browser and type the server or system Ip address where you have executed the above command. It will show the process to set up the WordPress website including username and password for the same.

Wordfpress instaaltion script with LAMp on ubuntu 20.04 LTS

Optional:

In case you need phpMyAdmin, then use the below command after installing WordPress to easily handle databases.

apt install phpmyadmin

 

 

13 thoughts on “Script to install LAMP & WordPress on Ubuntu 20.04 LTS server quickly with one command”

  1. Hi can you update your script please. after installation and typing localhost to webbrowser. I have
    <?php
    /**
    * Front to the WordPress application. This file doesn't do anything, but loads
    * wp-blog-header.php which does and tells WordPress to load the theme.
    *
    * @package WordPress
    */

    /**
    * Tells WordPress to load the WordPress theme and output it.
    *
    * @var bool
    */
    define( 'WP_USE_THEMES', true );

    /** Loads the WordPress Environment and Template */
    require __DIR__ . '/wp-blog-header.php';

    Reply
    • Hello Sergi, the script has been updated, now you can run it again. However, make sure to remove earlier installation or go for a fresh server.

      Reply
  2. Script finished downloading latest wp release, and errors at line 68: /usr/bin/lynx: not found
    Proceeds to run echo of DB and pass

    Reply
  3. Thanks for the script. I am trying to update it to include more things like lets encrypt certs and other WordPress needs. When I have an update, I can share it. Thank you.

    Reply
  4. Hello, I proceeded the steps and it gave me the database info, now when I enter the IP or the DNS related to it, nothing happens!

    what shall I do to keep continuing the installation?

    Thanks

    Reply
  5. probably there is some problem with installing the Apache service and I tried a lot, but no result!

    if you have any solution for that! I’m eager to try! thanks

    —————————————–
    log:

    Aug 08 07:22:21 srv9810699945 systemd[1]: apache2.service: Control process exite d, code=exited, status=1/FAILURE
    Aug 08 07:22:21 srv9810699945 systemd[1]: apache2.service: Failed with result ‘e xit-code’.
    Aug 08 07:22:21 srv9810699945 systemd[1]: Failed to start The Apache HTTP Server

    Reply
  6. This mostly works on a new install. If something goes wrong and even remove MySQL, php, and apache2 and WordPress.

    This script will not work on installing it back.

    But it works super good on a new install.

    -Raymond Day

    Reply
  7. Because you made this very good script can you make one that would copy the Database to another WordPress install so it have to change the URL in the Database and to only save the changes. Because if on a SSD saving the hole Database over it’s salve would wear it out faster.

    This would be great if you could do a script to do that because could use a cron job to back it up to another server with WordPress.

    Thank you.

    Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.