Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Move Magento from Localhost to VPS with Nginx

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

This guide walks you through moving a Magento 2 store from your local development environment to a VPS running Nginx. You'll learn how to prepare your local files and database, transfer them securely, configure Nginx and PHP, and update Magento's base URLs so your store works correctly in production. Before you begin, ensure you have SSH access to your VPS, a domain name pointing to its IP, and root or sudo privileges.

What You'll Need

  • A VPS with Nginx, PHP 7.4 or newer, and MySQL/MariaDB installed
  • SSH client (like PuTTY on Windows or Terminal on macOS/Linux)
  • Access to your local Magento installation (files and database)
  • A domain name or subdomain pointing to your VPS IP address
  • Composer and Git installed on the VPS (if you use them)

Step-by-Step

  1. Backup your local Magento files. On your local machine, navigate to your Magento root folder and create an archive of all files, excluding the var/cache and var/session directories to reduce size. Use a command like tar -czf magento.tar.gz --exclude='var/cache' --exclude='var/session' . from inside the Magento root.
  2. Export your local database. Use mysqldump to create a SQL dump of your Magento database. For example: mysqldump -u root -p magento_local > magento_local.sql. Ensure you have the correct database name and credentials.
  3. Upload files to the VPS. Use SCP or SFTP to copy the archive and SQL dump to your VPS. For example: scp magento.tar.gz magento_local.sql user@your-vps-ip:/var/www/. Replace user and your-vps-ip with your actual SSH user and VPS IP.
  4. Extract files on the VPS. SSH into your VPS and move the archive to the desired web root (e.g., /var/www/magento). Extract it with tar -xzf magento.tar.gz. Ensure the directory is owned by the web server user (e.g., www-data): sudo chown -R www-data:www-data /var/www/magento.
  5. Import the database. Create a new database and user on the VPS, then import the SQL dump: mysql -u root -p -e 'CREATE DATABASE magento_prod;' and mysql -u root -p magento_prod < magento_local.sql. Grant privileges to a dedicated user.
  6. Update app/etc/env.php. Edit this file on the VPS to reflect the new database credentials (host, dbname, username, password). Also update the cache and session settings if needed.
  7. Update base URLs. In the core_config_data table, replace http://localhost/magento with your production URL. Run SQL: UPDATE core_config_data SET value='https://yourdomain.com/' WHERE path IN ('web/unsecure/base_url','web/secure/base_url'); Also update the web/unsecure/base_link_url and web/secure/base_link_url paths.
  8. Configure Nginx. Create a server block (virtual host) for your domain. A basic configuration includes server_name yourdomain.com;, root /var/www/magento/pub;, and index index.php;. Include the Magento Nginx configuration by adding include /var/www/magento/nginx.conf.sample; inside the server block. Then enable the site and reload Nginx: sudo ln -s /etc/nginx/sites-available/magento /etc/nginx/sites-enabled/ and sudo systemctl reload nginx.
  9. Set file permissions. Run the following commands to set proper permissions: sudo find var generated vendor pub/static pub/media app/etc -type f -exec chmod g+w {} + and sudo find var generated vendor pub/static pub/media app/etc -type d -exec chmod g+ws {} +. Also set sudo chown -R :www-data . in the Magento root.
  10. Run Magento commands. From the Magento root, run php bin/magento setup:upgrade, then php bin/magento setup:di:compile, and php bin/magento setup:static-content:deploy -f. Finally, clear the cache with php bin/magento cache:clean.
  11. Test your site. Visit your domain in a browser. Check that the homepage loads, product pages work, and the admin panel is accessible. If you see a 404, check the Nginx configuration and the pub directory path.

Common Mistakes

  • Wrong file ownership or permissions. Magento requires the web server user to own and write to specific directories. If you skip the chown/chmod steps, you'll get blank pages or write errors.
  • Forgetting to update base URLs. If you only update env.php but not the database, your site will still redirect to localhost.
  • Using incorrect Nginx root. Magento 2 uses pub as the document root, not the Magento root. Pointing Nginx to the wrong directory causes 404 errors.
  • Not clearing the cache after moving. Old cached data can cause issues, especially if the cache was stored in files. Always run cache:clean after the move.

Tips

  • Use a staging subdomain first. Test the move on a subdomain like staging.yourdomain.com before switching the main domain to avoid downtime.
  • Enable Redis or Varnish. For production, consider using Redis for session and cache storage to improve performance. Update env.php accordingly.
  • Set up SSL. Use Let's Encrypt to get a free SSL certificate. Update the secure base URLs to https:// and force HTTPS in Nginx.
  • Back up the VPS database after import. Before making further changes, take a fresh dump of the imported database so you have a known good state.

Troubleshooting

Blank page or 500 error. Check the PHP error log (e.g., /var/log/nginx/error.log and /var/log/php7.4-fpm.log). Common issues include missing PHP extensions (like bcmath or intl) or incorrect file permissions. Run php bin/magento info:adminuri to verify the admin URL.

404 errors on all pages. Ensure Nginx is pointing to the pub directory and that the nginx.conf.sample is included. Also check that server_name matches your domain exactly.

CSS/JS not loading. Run php bin/magento setup:static-content:deploy -f and ensure the pub/static directory has write permissions for the web server user.

Database connection errors. Double-check the credentials in env.php. Test the connection with mysql -u user -p -h host dbname from the VPS.

Admin panel redirects to localhost. The base URLs in the database are still pointing to localhost. Run the SQL update again, and also check the web/secure/base_url and web/unsecure/base_url paths in core_config_data.

Frequently Asked Questions

Can I move Magento 1 with this guide?

No, this guide is for Magento 2. Magento 1 has a different structure and Nginx configuration, and it's end-of-life, so an upgrade is recommended.

Do I need to change the pub directory permissions?

Yes, the web server needs write access to pub/static and pub/media for static content deployment and image uploads. The chmod commands in Step 9 handle this.

What if my domain uses HTTP instead of HTTPS?

You can set the base URLs to http:// in the database, but it's strongly recommended to use HTTPS for security. If you don't have SSL yet, you can temporarily use HTTP.

How do I change the admin URL after moving?

Use php bin/magento setup:config:set --backend-frontname='admin_newpath' to change it, then clear the cache.

Can I use a different database name on the VPS?

Yes, just update the dbname in env.php accordingly. The import will work regardless of the database name.

What if my local files are too large to upload?

Use rsync over SSH to transfer files incrementally. For example: rsync -avz --exclude='var/cache' --exclude='var/session' /path/to/magento/ user@vps:/var/www/magento/.