Script: Install WordPress on a Debian/Ubuntu VPS

Previously we explained how to install WordPress on a Debian VPS. Also you can install WordPress on Debian or Ubuntu VPS in an easier way, using the script provided in this article. This script will create a MySQL database, will download and configure the latest WordPress version and create Apache virtual host for you automatically. All you need to do is to create a file on your WordPress VPS with the content shown below, make the file executable, execute it and enter a few parameters.

Create a new file and paste the script:

# nano wpinstall
#!/bin/bash
#
# Install WordPress on a Debian/Ubuntu VPS
#

# Create MySQL database
read -p "Enter your MySQL root password: " rootpass
read -p "Database name: " dbname
read -p "Database username: " dbuser
read -p "Enter a password for user $dbuser: " userpass
echo "CREATE DATABASE $dbname;" | mysql -u root -p$rootpass
echo "CREATE USER '$dbuser'@'localhost' IDENTIFIED BY '$userpass';" | mysql -u root -p$rootpass
echo "GRANT ALL PRIVILEGES ON $dbname.* TO '$dbuser'@'localhost';" | mysql -u root -p$rootpass
echo "FLUSH PRIVILEGES;" | mysql -u root -p$rootpass
echo "New MySQL database is successfully created"

# Download, unpack and configure WordPress
read -r -p "Enter your WordPress URL? [e.g. mywebsite.com]: " wpURL
wget -q -O - "http://wordpress.org/latest.tar.gz" | tar -xzf - -C /var/www --transform s/wordpress/$wpURL/
chown www-data: -R /var/www/$wpURL && cd /var/www/$wpURL
cp wp-config-sample.php wp-config.php
chmod 640 wp-config.php
mkdir uploads
sed -i "s/database_name_here/$dbname/;s/username_here/$dbuser/;s/password_here/$userpass/" wp-config.php

# Create Apache virtual host
echo "
ServerName $wpURL
ServerAlias www.$wpURL
DocumentRoot /var/www/$wpURL
DirectoryIndex index.php

Options FollowSymLinks
AllowOverride All

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
" > /etc/apache2/sites-available/$wpURL

# Enable the site
a2ensite $wpURL
service apache2 restart

# Output
WPVER=$(grep "wp_version = " /var/www/$wpURL/wp-includes/version.php |awk -F\' '{print $2}')
echo -e "\nWordPress version $WPVER is successfully installed!"
echo -en "\aPlease go to http://$wpURL and finish the installation\n"

Make the script executable:

# chmod +x wpinstall

Execute the script:

# ./wpinstall

For updates, you can also read our post on How to Install WordPress with Nginx on Debian 10.

15 thoughts on “Script: Install WordPress on a Debian/Ubuntu VPS”

  1. Fantastic!!!

    friend was on the fence trying to decide on VPS, this might just push him over.
    Also a script for UFW, to change ports, and webmin/virtual min instal would be nice
    referred a friend to host with you.

    Reply
  2. That’s a good script. I was writing my own and decided to see how many others I could find.
    The question I have here, is WP Authentication Unique Keys and Salts. Those are going to be blank if you just copy over the sample wordpress config file. Unless I’m missing something when its ran the 1st time.

    Reply
    • You could generate the authentication unique keys and salts using WordPress secret key generator at https://api.wordpress.org/secret-key/1.1/salt/ and add them to your wp-config.php file.

      Reply
  3. Thanks very much for this script, used it successfully to automate WordPress installs, the only change I’d suggest is telling users they may need to run …

    sudo ./wpinstall

    so that all the permissions are setup.

    Reply
  4. just found this — nice script. any ideas what expression to use to include the SALT text in the file?

    salt=”$( curl https://api.wordpress.org/secret-key/1.1/salt/ )”
    sed -i “s/[some REGEX I’m not sure] /$salt/” wp-config.php

    Reply
  5. Is there an error in the script? Should it be:

    echo “GRANT ALL PRIVILEGES ON $dbname.* TO ‘$dbuser’@’localhost’

    and not

    echo “GRANT ALL PRIVILEGES ON $dbname.* TO ‘$bduser’@’localhost’

    ?

    Reply
  6. hi, need to know details about manually if we want to access all of installed of this script, cause i’m new on server admin.

    also, when i setup domain, they said domain not exsist. how can fix this?

    Reply
    • Hi Juanita,

      The script installs WordPress in the ‘/var/www/YOURDOMAN’ directory on your server, so you can access the WordPress files in this directory.

      Can you please be more specific about the domain setup? Where exactly you are getting the ‘domain not exist’ message?

      Reply
    • We can help you with this setup. Feel free to contact us via live chat or send an email to support@rosehosting.com for more information.

      Reply
  7. Using your script which is a great help, but I’m find that its creating a virtual site, and having to add it afterwards.

    Reply
    • If you don’t want the virtual host to be created after running the script you can remove the “#Create Apache virtual host until #Enable the site” area.

      Reply

Leave a Comment