Skip to content

Instantly share code, notes, and snippets.

Created August 24, 2023 08:28
Show Gist options
  • Save gsidhu/18a48311ef82912fc6dc60530c998767 to your computer and use it in GitHub Desktop.
Save gsidhu/18a48311ef82912fc6dc60530c998767 to your computer and use it in GitHub Desktop.
Somebody sent you a WordPress site in a zip file. You want to run it locally. How do you do it?

What I received:

  1. A zip file containing a WordPress site.
  2. A .sql database file.

I figured that the .sql file is the WordPress database.

How to get shit running:

  1. Set up an Ubuntu virtual machine. Why virtual machine? So you get a contained sandbox and a clean slate for your experiments. Why Ubuntu? It's just a whole lot easier to do these things on Linux. Screw your MAMP and WAMP stacks.

  2. Install nginx, mysql-server and php7.4. That's your LEMP stack right there.1 DigitalOcean tutorials can help you set these things up. Use php7.4 because most of the Internet breaks on php8.1. You'll need to add a new ppa package for that - DigitalOcean can tell you how. All the additional php packages that you install should also have the php7.4 prefix in front of them. If you install php-fpm you might just get php8.1-fpm. So install php7.4-fpm instead.

  1. Read the wp-config.php file in your site folder to get the database name and user credentials.

  2. Create a new database in mysql with that name. Then add a user with that password to that database. Then grant access to that user over that database. Again, DigitalOcean has your back.

  3. Now open mysql as this user (mysql -u USER -p), open the database (USE DATABASENAME;), and then import the .sql file using: source /path/to/database.sql. Wait while it imports everything. You can check if it worked by running SHOW TABLES; and seeing if it lists all the tables in the database. If shit breaks, you are on your own - go figure.

  4. At this point your database is ready. Now you just have to copy your site over and tell Nginx to start redirecting localhost traffic to it.

  5. Assuming you have copied your site to /var/www/wordpress/, follow DigitalOcean's instructions to create a site config in the /etc/nginx/sites-available/ directory. You can get the complete server config in this DigitalOcean tutorial. Make sure that the php-fpm version is set correctly in the server config. Also make sure that you have pointed the root in the right direction (which is /var/www/wordpress in this case).

  6. Finish things off by creating a symlink (ln -s) to the sites-enabled directory, running nginx -t and restarting nginx (systemctl restart nginx). Note: you might have a 'default' config in the sites-enabled directory. You can delete that. Otherwise your localhost on port 80 (default port) will point to that. You want it to point to your WordPress site instead. Or you could change your WordPress config to listen to traffic on port 81 or whatever. Up to you.

  7. Now you should be able to access your WordPress site on localhost. If it doesn't load, something is wrong with your nginx config. Go back to step 7. If it loads and the site runs as intended, congratulations. Go to /wp-admin/ and have your fun.

If it doesn't, well here we go, lad.

  1. Enable debugging in your config file. Add the following lines to your wp-config.php file:
define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );
  1. Now try to access your site again. If it breaks, you should probably see it in the browser. Or see the debug.log file in your site's root directory. Open it and see what's wrong.

  2. Most likely it's a theme or plugins that are broken. Simply rename the plugins folder to plugins.hold (sudo mv /var/www/wordpress/wp-content/plugins /var/www/wordpress/wp-content/plugins.hold). Now try to access the site again. If it works, great, go to your admin dashboard and see if there are updates to plugins available and then activate them one by one and hope they work.

  3. If when you try to update plugins, WordPress says it needs FTP access to your server or something, simply add this line to your wp-config.php file: define('FS_METHOD', 'direct');. This will allow WordPress to update plugins directly without needing FTP access. You'll also need to give the www-data user ownership of the site directory:

sudo chown -R www-data:www-data /var/www/wordpress
sudo find /var/www/wordpress -type d -exec chmod 755 {} \;
sudo find /var/www/wordpress -type f -exec chmod 644 {} \;
  1. If plugins aren't it, then it's probably a theme. Try disabling the themes one by one. Or disable all themes except the WordPress default theme (usually called twentytwentythree or something like that). You'll also have to change the theme option for your site in the database. You can do that by running the following command in mysql (make sure you are in the right database):
UPDATE wp_options SET option_value = 'twentytwentythree' WHERE option_name = 'template';
UPDATE wp_options SET option_value = 'twentytwentythree' WHERE option_name = 'stylesheet';

If you get an error saying wp_options does not exist, then it's probably because the database prefix in your config file is set to something else. Let's say the prefix is "bowow" so your table will then be called bowow_options. Change the SQL commands accordingly.

  1. Now go to localhost in the browser and it should most likely work. Then figure out how to get the broken theme(s) to work from there.

  2. One more thing, the default site URL may be set to something else. You'll need to change that to http://localhost as well. You can do that by running the following command in mysql (make sure you are in the right database and use the right table name):

UPDATE wp_options SET option_value = 'http://localhost' WHERE option_name = 'siteurl';
UPDATE wp_options SET option_value = 'http://localhost' WHERE option_name = 'home';
  1. Lastly, if you can't log into /wp-admin because you don't know what user credentials to use, you can create a new user or just find the existing admin user and change its password. To do that, first generate an MD5 hash for your new password (this tool can help). Then go into mysql one more time, find the user name in the wp_users table and modify its password:
SELECT * FROM wp_users; // find the user_login corresponding to the admin user

Now you can go to /wp-admin and log in with the new password for the admin user.


  1. LEMP >> LAMP. Apache doesn't make sense to me, I am an Nginx guy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment