WordPress / 7 MIN READ
Servering WP local
How to serve WordPress locally
From the original Fervor library. Examples may use older package versions.
Creating a local copy of your WordPress (WP) site is a valuable way to develop, test, and experiment without affecting your live website. This tutorial will guide you through the process of exporting your WP site and setting it up locally on your computer. We’ll cover all the essential steps, including exporting files and the database, setting up a local server environment, and configuring your local site.
Table of Contents
- Table of Contents
- Prerequisites
- Step 1: Export WordPress Files
- Step 2: Export the Database
- Step 3: Set Up a Local Server Environment
- Step 4: Create a Local Database
- Step 5: Import the Database
- Step 6: Configure wp-config.php
- Step 7: Update Site URLs
- Step 8: Transfer Files to Local Server
- Step 9: Test the Local Site
- Alternative Method: Using a Migration Plugin
- Conclusion
Prerequisites
Before you begin, ensure you have the following:
-
Access to Your Live WordPress Site:
- FTP/SFTP access to download site files.
- Access to your hosting control panel (e.g., cPanel) to export the database via phpMyAdmin.
-
Local Server Software:
- For Windows: XAMPP or WAMP.
- For macOS: MAMP or Local by Flywheel.
- For Linux: LAMP stack or XAMPP.
-
A Database Management Tool: phpMyAdmin is commonly bundled with local server packages.
-
Basic Knowledge of FTP and Databases: Familiarity with using FTP clients (like FileZilla) and phpMyAdmin will be helpful.
Step 1: Export WordPress Files
Your WordPress site consists of core files, themes, plugins, and media uploads. To export these:
-
Connect to Your Server via FTP:
- Use an FTP client like FileZilla to connect to your live server.
- Enter your FTP credentials (host, username, password, port).
-
Download the WordPress Directory:
- Navigate to the directory where WordPress is installed (commonly
public_html,www, or a subfolder). - Download all files and folders to a local directory on your computer. Ensure you include hidden files like
.htaccess.
- Navigate to the directory where WordPress is installed (commonly
Alternative Method Using cPanel File Manager:
- Log in to your hosting control panel (e.g., cPanel).
- Navigate to File Manager.
- Compress the WordPress directory into a
.zipfile. - Download the compressed file to your computer.
- Extract the
.zipfile locally.
Step 2: Export the Database
WordPress uses a MySQL database to store content, settings, and other dynamic data.
-
Access phpMyAdmin:
- Log in to your hosting control panel (e.g., cPanel).
- Navigate to phpMyAdmin.
-
Select Your WordPress Database:
- In phpMyAdmin, locate and select the database used by your WordPress site. If unsure, check the
wp-config.phpfile for theDB_NAMEvalue.
- In phpMyAdmin, locate and select the database used by your WordPress site. If unsure, check the
-
Export the Database:
- Click on the Export tab.
- Choose the Quick export method and SQL format.
- Click Go to download the
.sqlfile to your computer.
Note: For larger databases or more control, use the Custom export method to select specific tables or adjust export settings.
Step 3: Set Up a Local Server Environment
To run WordPress locally, you need a local server environment that includes Apache (or another web server), PHP, and MySQL.
-
Download and Install Local Server Software:
- XAMPP: Download XAMPP and follow installation instructions.
- MAMP: Download MAMP for macOS.
- WAMP: Download WAMP for Windows.
- Local by Flywheel: Download Local for an easy WordPress local setup.
-
Start the Local Server:
- Launch the local server application.
- Start Apache and MySQL (or the equivalent services).
Step 4: Create a Local Database
-
Access phpMyAdmin Locally:
- Open your web browser and navigate to
http://localhost/phpmyadmin/(adjust based on your local server setup).
- Open your web browser and navigate to
-
Create a New Database:
- Click on the Databases tab.
- Enter a name for your local WordPress database (e.g.,
wordpress_local). - Choose the utf8mb4_unicode_ci collation.
- Click Create.
Step 5: Import the Database
-
Select the Newly Created Database:
- In phpMyAdmin, click on your local database (
wordpress_local).
- In phpMyAdmin, click on your local database (
-
Import the SQL File:
- Click on the Import tab.
- Click Choose File and select the
.sqlfile you exported earlier. - Ensure the format is set to SQL.
- Click Go to start the import process.
Note: If your database is large, you may need to adjust the upload limits in your local server’s PHP configuration.
Step 6: Configure wp-config.php
-
Locate wp-config.php:
- In your downloaded WordPress files, find the
wp-config.phpfile.
- In your downloaded WordPress files, find the
-
Edit Database Credentials:
- Open
wp-config.phpin a text editor. - Update the following lines with your local database details:
define('DB_NAME', 'wordpress_local'); // Your local database name define('DB_USER', 'root'); // Typically 'root' for local environments define('DB_PASSWORD', ''); // Typically empty for local environments define('DB_HOST', 'localhost'); // Usually 'localhost'Example:
define('DB_NAME', 'wordpress_local'); define('DB_USER', 'root'); define('DB_PASSWORD', ''); define('DB_HOST', 'localhost'); - Open
-
Save Changes:
- Save and close
wp-config.php.
- Save and close
Step 7: Update Site URLs
To ensure your local site works correctly, you need to update the site URLs in the database.
Method 1: Using phpMyAdmin
-
Access phpMyAdmin:
- Navigate to
http://localhost/phpmyadmin/.
- Navigate to
-
Select Your Local Database:
- Click on
wordpress_local(or your database name).
- Click on
-
Navigate to the wp_options Table:
- Click on the
wp_optionstable. (Note: The table prefix might differ, e.g.,wp123_options.)
- Click on the
-
Edit siteurl and home:
- Locate the
siteurlandhomerows (usually IDs 1 and 2). - Click Edit for each and change the URL to
http://localhost/your-folder(replaceyour-folderwith your local directory name, if applicable).
Example:
siteurl:http://localhost/wordpresshome:http://localhost/wordpress
- Locate the
-
Save Changes:
- Click Go to save each change.
Method 2: Using Search and Replace Tool
For serialized data and to update URLs throughout the database, consider using a tool like WP-CLI or plugins like Better Search Replace.
Step 8: Transfer Files to Local Server
-
Locate the Local Server’s Web Directory:
- XAMPP:
C:\xampp\htdocs\(Windows) or/Applications/XAMPP/htdocs/(macOS). - MAMP:
/Applications/MAMP/htdocs/. - WAMP:
C:\wamp64\www\.
- XAMPP:
-
Create a New Folder:
- Create a new folder within the web directory (e.g.,
wordpress).
- Create a new folder within the web directory (e.g.,
-
Copy WordPress Files:
- Copy all the downloaded WordPress files from Step 1 into the new folder.
Example Path:
C:\xampp\htdocs\wordpress\
Step 9: Test the Local Site
-
Access the Local Site:
- Open your web browser and navigate to
http://localhost/wordpress/(replacewordpresswith your folder name).
- Open your web browser and navigate to
-
Login to WordPress Dashboard:
- Go to
http://localhost/wordpress/wp-admin/. - Use your existing WordPress admin credentials.
- Go to
-
Permalinks:
- To fix permalink issues, go to Settings > Permalinks in the dashboard.
- Without making changes, click Save Changes to regenerate the
.htaccessfile.
-
Check Functionality:
- Navigate through your site to ensure everything works as expected.
- Test themes, plugins, and media uploads.
Troubleshooting Tips:
-
White Screen or Errors:
- Check your local server’s error logs.
- Ensure PHP versions are compatible.
-
Broken Links or Missing Images:
- Verify that media files are correctly copied.
- Use search and replace tools to update any remaining URLs.
Alternative Method: Using a Migration Plugin
If you prefer a streamlined process, consider using a migration plugin like Duplicator or All-in-One WP Migration.
Using Duplicator:
-
Install and Activate Duplicator on Live Site:
- Go to Plugins > Add New.
- Search for Duplicator, install, and activate it.
-
Create a Package:
- Navigate to Duplicator > Packages.
- Click Create New, follow the steps, and build the package.
-
Download Package and Installer:
- Once built, download both the
.zippackage andinstaller.phpfile.
- Once built, download both the
-
Set Up Local Server:
- Create a new folder in your local server’s web directory.
- Place both the package and
installer.phpin this folder.
-
Create Local Database:
- Follow Steps 3 and 4 to set up a local database.
-
Run the Installer:
- In your browser, navigate to
http://localhost/your-folder/installer.php. - Follow the on-screen instructions to complete the installation.
- In your browser, navigate to
-
Test the Local Site:
- Log in and verify that the site functions correctly.
Conclusion
Setting up a local copy of your WordPress site enables you to develop and test changes safely. By following this tutorial, you’ve successfully exported your live site and configured it to run locally. Whether you choose the manual method or utilize a migration plugin, maintaining a local development environment is a best practice for WordPress site management.
Additional Tips:
- Regular Updates: Keep both your live and local environments updated to minimize discrepancies.
- Version Control: Consider using version control systems like Git for tracking changes.
- Backups: Always back up your site before making significant changes or migrations.