29 October 2020

Deploying a WordPress Site to Live Server

Assumptions

  • Domain name is pointed at server.
  • Folder is created on server where the site will live.

Set Up

  • XAMPP on local machine
  • Linux Server running Apache

The Process

  1. Copy site folder with permissions
  2. Download database and edit URLs for live sever
  3. Transfer files to live server
  4. Create database on live server and upload database
  5. Edit wp-config and .htaccess
  6. Edit database wp_options table URLs
  7. Login and check uploads and permalinks

1. Copy site folder with permissions

Depending on your set up you can either Tar Gzip everything from the command line, just use a normal zip, or if you feel like you have time to waste FTP the files accross directly.

/* Run this from the command line inside the main folder */

shopt -s dotglob && tar -czvpf websitename.tar.gz . && shopt -u dotglob

2. Download database and edit URLs for live sever

I like to use phpmyadmin for all database interactions but you can equally use the command line. Download the database and load it into a text editor. We are going to find and replace all the URLs, which are probably http://localhost/websitename and change them to the new web address https://websitename.com.

If you want to skip this step you can use an SQL query to change the URLs once the database has been uploaded onto the live server instead.

3. Transfer files to live server

You should have a folder on your server where the live site is hosted with same name as the web address for the site. The location of the folder can vary from host to host, some maybe public/htdocs/websitename.com, others var/www/public/websitename.com, and others public_html/websitename.com.

Use the command line or FTP/SFTP to upload the files into the folder and unzip if needed.

/* Run this from the command line inside the main folder to unzip using tar */

tar -xzvpf websitename.tar.gz


/* Or using normal zip */

unzip websitename.zip

4. Create database on live server and upload database

Using phpmyadmin create a new user with all permissions and a database with the same name, for ease. Keep a note of the password. You will need to enter all these details into the wp-config file.

Import the database with the newly edited URLs.

If you didn’t use a text editor to change the URLs you can run this SQL query from the phpmyadmin panel to update all the URLs at once.

/* Update WordPress URLs in database */

UPDATE wp_options SET option_value = replace(option_value, 'http://www.old-domain.com', 'http://www.new-domain.com') WHERE option_name = 'home' OR option_name = 'siteurl';
UPDATE wp_posts SET guid = replace(guid, 'http://www.old-domain.com','http://www.new-domain.com');
UPDATE wp_posts SET post_content = replace(post_content, 'http://www.old-domain.com', 'http://www.new-domain.com');
UPDATE wp_postmeta SET meta_value = replace(meta_value, 'http://www.old-domain.com', 'http://www.new-domain.com');

5. Edit wp-config and .htaccess

Open the wp-config.php and .htaccess file in the root of your new website. These details will need to be edited to connect the WordPress site to the database and, depending on your local set up, the domain pointing will need to be updated.

wp-config.php

The areas to edit here are the mySQL settings for the database and domain configuration. Also if you have not already done so generate new unique keys and salts

/** The name of the database for WordPress */
define( 'DB_NAME', 'website' );

/** MySQL database username */
define( 'DB_USER', 'root' );

/** MySQL database password */
define( 'DB_PASSWORD', '' );


/* Domain config */
define('WP_HOME', 'http://localhost/websitename');
define('WP_SITEURL', 'http://localhost/websitename');

/* Change to this or delete altogether */
define('WP_HOME', 'http://' . $_SERVER['HTTP_HOST']);
define('WP_SITEURL', 'http://' . $_SERVER['HTTP_HOST']);
.htaccess

Again here you should edit the .htaccess file if your localhost required the RewriteBase to be changed. You can also force https as an added bonus

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

/* Change the RewriteBase and RewriteRule if needed */

# 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

# Force https
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
</IfModule>

6. Edit database wp_options table URLs

Something that can sometimes trip you up when moving WordPress sites is the wp_options site and home URLs. If these do not match your new domain name the site will not connect properly, producing a white screen of death, and leaves many developers scratching their heads.

Open the database in phpmyadmin and select the wp_options table. The first two fields are the home and site URLs which can be edited by simply double clicking on them.

If you ran the SQL query to update the URLs you shouldn’t have to do this.

7. Login and check uploads and permalinks

The moment of truth. Login to the site via websitename.com/wp-admin or websitename.com/wp-login.php and make sure you can upload to the media library.

If this is throwing an error then the permissions need to be updated for the uploads folder so it has write permission. This can be done via your FTP client if you have admin rights or the command line.

Correct permission settings

755 for all folders and sub-folders.
644 for all files.

Useful information on permissions and ownership

Below is an example of setting all permissions to the correct value in a bytemark server via the command line, you will need to be logged in to the server as the root user (su):

chown www-data:www-data /srv/websitename.com/public/htdocs/
chown -R www-data:www-data /srv/websitename.com/public/htdocs/*
chown www-data:www-data /srv/websitename.com/public/htdocs/.htaccess
chmod g+w /srv/websitename.com/public/htdocs/
chmod -R g+w /srv/websitename.com/public/htdocs/*
chmod g+w /srv/websitename.com/public/htdocs/.htaccess


chmod +x /srv/websitename.com/public/htdocs/.htaccess


Refresh the permalinks by saving them, just to be safe.

utopianfool

Creative Web Developer

View all posts by utopianfool →