Download and Install WordPress via the Shell Over SSH

Download and Install WordPress via the Shell Over SSH

Tutorial Details
  • Program: WordPress
  • Difficulty: Medium
  • Estimated Completion Time: 10 minutes

Normally you install WordPress by downloading the ZIP file, uncompressing it on your PC, going into the wordpress subfolder, uploading and running the script and not to mention going to your host’s control panel to get the database setup and everything. Others do it by using Fantastico or SimpleInstall utilities available via their host’s control panel. There is nothing wrong with those methods except the first one takes quite some time and the second method is not foolproof to say the least.

Nowdays most hosting plans offer an SSH/command-line facility so you should definitely consider the SSH method to download and install WordPress on your server. The big advantage you get is that you don’t need to visit different sites, don’t need to do any download/upload or open any control panels. You do that all via a single command-line interface. For this you will need an SSH client. If you are running on Windows go for PuTTY and if you are on Mac you can use Mac’s built-in Terminal or iTerm 2. Before we move ahead please make sure your host offers bash shell because our commands are configured for that.


Step 1 Connecting to Your Server

Using PuTTY

Open PuTTY and enter your domain name in the box named ‘Host name(or IP address)’ and enter the port number used to connect to SSH under ‘Port’ and now click Open. You can even save your site settings by entering a name in the Saved Sessions box and by pressing Save. Next time you can always load the session by selecting your site and clicking Load.

PuTTY will now ask for your username. Enter your username and press enter. Now you will be asked for your password. Note here that while you are typing your password, you won’t see it being typed on the screen. It’s hidden for security reasons. Press enter after you’ve typed your password and you will be logged on.

Using any other SSH client or Mac Terminal

Enter the following command in your Terminal client to connect to your site’s command-line over SSH:

ssh username@domain.com -p 22

The -p switch tells it to use the port number 22. If your host allows SSH over default port 22, you can omit -p and 22 in the above command, otherwise substitute 22 for your host’s SSH port number. After logging in you will see something like this:

domain.com@username:-$

That is the shell command prompt where you will be typing all your commands from now on.


Step 2 Downloading WordPress

Now that we have logged in to our SSH server, we first need to go to the correct directory where we want to setup our blog and need to download the files and extract them there. Supposing the directory you want your blog to be installed under is blogdemo residing under the public_html directory, you will use the following command:

cd public_html/blogdemo/

Now that we have reached the correct directory we will download WordPress using the ‘wget’ command.

wget http://wordpress.org/latest.tar.gz
tar xfz latest.tar.gz

The above command downloads the latest WordPress install from their server and extracts the file from it into the blogdemo directory. x, f, and z are parameters which tell the tar command to extract the contents, tell that archive filename is given on the command-line and that gzip should be used for extraction respectively.

Now all archive files from WordPress have the installation files under a wordpress directory and after extraction you will find a wordpress directory under the blogdemo directory containing your install. So to shift the files back to where they should be use the following commands:

mv wordpress/* ./

This command moves the contents of the wordpress directory into the current directory. Anytime you want to check what the current directory consists of, type ‘ls’.

You can delete both the wordpress directory and the archive file you downloaded if you want using the following commands:

rmdir ./wordpress/
rm -f latest.tar.gz

Step 3 Installing WordPress

In this step we will create the database and corresponding user and associate them together. Then we will use the famous 5 minute install of WordPress to finish it off.

Note: Before moving ahead you will need to check whether you have got the privileges to create a database or not. An easy way to check is to go to your phpMyAdmin and check whether you can create a database from there or not. If you can’t that means you won’t be able to follow this step. You should check with your web host if they allow you to do so or not. This happens mostly on shared web hosting.

First you need to login to the MySQL command-line using the following command:

mysql -u username -p

After entering this you will be asked for your MySQL password. Type your password and you will be shown a screen like this:

Now that we have logged in to the MySQL Server, we will first create a database and will grant access to the user to that database. Use the following commands now:

create database dbname;
grant usage on *.* to username@localhost identified by 'password';
grant all privileges on dbname.* to username@localhost;

Don’t forget the semi-colon at the end of each MySQL command. The first command creates the database. The second command allows the user to connect to the database. The final command grants all privileges to the user for that database. You can test whether your database creation was successful by running this command:

use dbname;

It should say database changed. Now you can exit the MySQL command-line by typing ‘exit’.

Now fire up the blog in your browser and run the usual WordPress install and use the database information we used in the third step to setup your wp-config.php and then setup your blog.

Things to Note

There are two things I would like everyone to take note of in the above tutorial:

  • New Database User – In our tutorial we are using an already existing database user to connect to the new database. But in case you want separate user for each database you need to create a new user to access that database. Here is how you should do it.

    Once you are inside the MySQL shell, use the following commands to create a new user and set its password.

    mysql> create user 'dbusername'@'localhost' identified by 'password';

    Now go back to Step 3 and run all other commands with this username.

  • Editing wp-config.php – In our tutorial I have told you that after doing everything on the shell you can directly proceed to the install. But some of you might want to edit wp-config.php to add special settings and code. You can only do that via the shell. While you are in your blog directory at the shell use the following command to fire up the Vim Editor (a command-line shell file editor)

    vi ./wp-config.php

    Now you will see something like what’s shown below:

    Press the ‘I’ key to enter into ‘insert mode’ and arrow keys to move around the file. Once you have made your edits, press the Esc key to exit ‘insert mode’. To exit Vim type ‘:’ (without the quotes) and then type “wq” and press enter, this will save your changes and quit Vim.

Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • http://lazaac.com lazaac

    wow! thanks a lot.. I bookmark this as later reference.. even I’m using cloud server and automation system on setup the server, this tutorial help a little bit on scripting and coding over ssh while install wp, like what I’m using right now..

  • Will

    An attempt at bash

    #!/bin/bash

    echo “Database Name: ”
    read -e dbname
    echo “Database User: ”
    read -e dbuser
    echo “Database Password: ”
    read -s dbpass

    echo “run install? (y/n)”
    read -e run

    if [ "$run" == n ] ; then
    exit
    else
    #download wordpress
    curl -O http://wordpress.org/latest.tar.gz

    #unzip wordpress
    tar -zxvf latest.tar.gz

    #change dir to wordpress
    cd wordpress

    #copy file to parent dir
    cp -rf . ..

    #move back to parent dir
    cd ..

    #remove files from wordpress folder
    rm -R wordpress

    #create wp config
    cp wp-config-sample.php wp-config.php

    #set database details with perl find and replace
    perl -pi -e “s/database_name_here/$dbname/g” wp-config.php
    perl -pi -e “s/username_here/$dbuser/g” wp-config.php
    perl -pi -e “s/password_here/$dbpass/g” wp-config.php

    #create uploads folder and set permissions
    mkdir wp-content/uploads
    chmod 777 wp-content/uploads

    #remove zip file
    rm latest.tar.gz

    #remove bash script
    #rm wp.sh
    fi

    • http://nspeaks.com Navjot Singh
      Author

      Awesome Idea with the automation of the complete procedure.

    • http://tinywp.in Pothi Kalimuthu

      chmod 777 wp-content/uploads

      Let’s not recommend 777 for security reasons. Let the user or webhost decide what’s the right permission for uploads, upgrade and related folders.

      • Will

        Very valid point! Any improvement are always welcome

        • http://www.wimbledon-it.co.uk bschwabe

          Probably as recommended in the wordpress codex:
          I quote:

          Changing file permissions
          If you have shell access to your server, you can change file permissions recursively with the following command:

          For Directories:

          find /path/to/your/wordpress/install/ -type d -exec chmod 755 {} \;
          For Files:

          find /path/to/your/wordpress/install/ -type f -exec chmod 644 {} \;

          Source: http://codex.wordpress.org/Hardening_WordPress

  • Pingback: Download and Install WordPress via the Shell Over SSH | Shadowtek | Hosting and Design Solutions

  • http://www.bluecon.org bluecon

    Thanks a lot for sharing information. ;)

  • http://freshroastcreative.tumblr.com/ Brian Notess

    Great overview! One caveat as someone who has done this on several different servers.

    File permissions: The can cause many headaches!

    Lately I’ve been doing Plesk WP installs which I know, admitting that will cause me to lose all of my developer street-cred.

    • http://thomasgbennett.com Thomas Bennett

      You should definitely cover permissions. If you download this and install with SSH you’re going to have headaches if you don’t understand permissions.

  • http://music.case.edu Sarah B.

    Just noticing, as Brian says above – no info on permissions in your article. WP requires the owner of WP to be the user running web services. So if you’re not careful your files could end up owned as your SSH user instead of the www user — updates, plugin installs, theme installs, etc will give you the dreaded FTP error screen. Make sure you chmod your wp folder/files to the web user – for linux it’s www-data, for hosted sites it can either be your user account or a system account (www, www-data, etc).

    • http://music.case.edu Sarah B.

      Sorry, I meant chown, not chmod :p

  • Pingback: WordPress for BlackBerry Gets 1000+ Downloads Per Day! | Open Knowledge

  • http://www.webmaster-source.com redwall_hp

    Also, don’t forget that you can chain commands together. Instead of doing this:

    wget http://wordpress.org/latest.tar.gz
    tar xfz latest.tar.gz

    You can do this:

    wget http://wordpress.org/latest.tar.gz && tar xfz latest.tar.gz

    Now it will download and unpack without you having to pay attention.

  • Rex

    Hey great, it helped me a lot. This tutorial is good for fresh installation, but I was also looking for transferring wordpress from old server to new. While googling I just found this tutorial, which explained me a lot

    http://webstutorial.com/configuring-vps-moving-wordpress-from-old-hosting-to-new-vps-part-2/featured

  • Jay

    Stupid question. Why do we need to do this id we can simply use softculous/installatron or any other script? Is there any benefit of installing wordpress via SSH ? Just curious

    • Rex

      @Jay, if your hosting doesn’t provides Cpanel or if you are using unmanaged hosting, then there is no other option rather then using SSH

    • http://wp.envato.com/ Japh Thomson
      Staff

      Lots of reasons! :)

      Just a few are so we can understand what goes on inside other scripts, to give us flexibility and control, and allow us to make our own scripts for automation, etc.

    • http://nspeaks.com Navjot Singh
      Author

      I would never recommend Fantastico/Softculous/Installatron type scripts provided by your hosting to install softwares.

      First of all they don’t provide updates as soon as they are released. You might be installing 3.3 WordPress from your control panel while 3.3.1 is already out there. I know once installed you can always update but why the extra procedure? Fantastico at present offers 3.1 version. Installing directly overcomes this. Also their install procedures are highly simplified and at times skip the important details you should know while installing. Like if installing via Fantastico, you will not be asked to set Search engine privacy option. This option comes handy if you are installing a blog for development purposes and not as a live website.

      You should always know how to install your scripts/softwares manually. Gives you better control and helps you a lot in the long run.

  • http://webmastersintexas.com Jerry Lee

    Hmm, it’s like dejavu.. Could have sworn I wrote this article… Good anyway.

    • http://wp.envato.com/ Japh Thomson
      Staff

      Hey Jerry, you did write a very similar article late last year, thanks!

      It’s always nice to post reminders, and slightly different ways of doing things :)

  • http://webmastersintexas.com Jerry Lee

    I agree, he added in the mysql information, which is nice.

  • http://www.parsinaweb.com Masoud

    it was a good tutorial , thank you

  • Pingback: Download and Install WordPress via the Shell Over SSH | 備忘録

  • http://ilikekillnerds.com Dwayne

    Not sure if it was just me, but I somehow wiped out my Linode server following these instructions. Like I said it might have been me, but I followed the instructions to a tee and somehow managed to delete some important files that caused me to rebuild from a backup.

    The step that ruined it all for me was:

    rmdir ./wordpress/
    rm -f latest.tar.gz

    Not making baseless accusations just letting everyone know in-case it was due to the instructions, but many here appear to not have had any problems so probably just me.

    • http://wp.envato.com/ Japh Thomson
      Staff

      Hi Dwayne, I’m really sorry to hear that happened! If you missed a step in the instructions or performed commands from the wrong directory, you could delete folders or files you didn’t intend to. But I can’t see from the commands you mentioned how that could have happened in this case.

      I hope you’re back up and running now!

  • http://www.SuccessinService.com Tony Locke

    so like, I am a complete newbie. I use Go Daddy and Plesk. I recently used “Backup Creator” to mirror/copy some WordPress installed sites off one server to a new dedicated virtual server. I have 71 domains I host and run for friends. Not a paid service as I am unreliable and early in my learning.

    So when I transferred the sites over to a new server, the database name and password were different and so the websites when down. Not sure why some sites transferred fine, and others were unable to get a server connection.

    Either way, I am solving this with a new WordPress install within Go Daddy and an upload installation of another backup I have.

    My trouble is that on three of the sites I can’t even update plugins. The permissions are wrong and files are not assigned to my user name. So I can’t add plugins or make database changes.

    I can use Putty to log into my server. I know the user name and password, but when I log into the IP and type “dir” it turns up nothing. According to Putty, there are no directories on the server. But all my websites that are working are still working.

    And if I log into the direct WordPress installation via the database user name and passowrd, there still isn’t any directories listed.

    All I want to do is change permissions so I can modify themes, etc.

    Help!!!

    When I log into the server using the IP, should I be able to find WordPress directories and files?

  • Pingback: Coding WordPress With Android | Wptuts+

  • Pingback: Coding WordPress With Android | Wordpress Webdesigner

  • Pingback: Coding WordPress With Android | Shadowtek Hosting and Design Solutions

  • Pingback: My Stream | Coding WordPress With Android | My Stream

  • Pingback: Coding WordPress With Android | How to Web

  • Pingback: Wordpress Leaks » Coding WordPress With Android

  • http://miasua.com BeximusPrime

    mv wordpress/* ./ doesn’t seem to work to move any hidden files, so with this tutorial the .htaccess file was left behind for me.

  • Pingback: Как разрабатывать для WordPress с помощью Android устройств | Wordpresso

  • Joel Davis

    Thanks for the tutorial.

    Your create user line has a minor syntax error. It is missing two apostrophes. It should be:

    mysql> create user ‘dbusername’@'localhost’ identified by ‘password’;

    • http://wp.envato.com/ Japh Thomson
      Staff

      Thanks for spotting that, Joel. I’ve updated the post now so others won’t have to work that out for themselves :)

  • Pingback: Quick Tip: Upgrade Your WordPress Site via SSH | Wptuts+

  • Pingback: Quick Tip: Upgrade Your WordPress Site via SSH | Wordpress Webdesigner

  • Pingback: My Stream | Quick Tip: Upgrade Your WordPress Site via SSH | My Stream

  • Pingback: Quick Tip: Upgrade Your WordPress Site via SSH « Fabián Couto Blog | www.fabiancouto.com.ar

  • cherubdawg

    Thank you for this. Followed the steps and everything worked perfectly. It’s incredible how much faster this is than uploading thousands of files via FTP.

  • http://www.facebook.com/mittul.chauhan Mittul Chauhan

    i have a wamp server .. can i test this method in localhost ?

    i have downloaded the PUTTY but then stuck .

    Added the host name 127.0.0.1 and port name as 80 .

    But then the black dialog opening and i can not write anything ?

    can we test PUTTY on localhost or local servers ?

    please tell me

  • Pingback: Test on 27 March | sunildas at tetragono