Skip to content

This repository contains a Docker project that allows you to expose your locally WordPress site online using Cloudflare Tunnel via TryCloudflare. Useful when you want to share a preview of the site under development with a client.

License

Notifications You must be signed in to change notification settings

alfiosalanitri/wordpress-local-tunnel

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

WordPress Local Tunnel with Docker and Cloudflare Argo

This repository contains a Docker Compose file that allows you to expose your locally developed WordPress site online using Cloudflare Tunnel via TryCloudflare.

Useful when you want to share a preview of the site under development with a client.

Prerequisites

Before using this script, ensure you have the following installed:

Features

  • Cloudflare Tunnel: Exposes your local WordPress site securely over the internet using Cloudflare's cloudflared service.
  • Custom WordPress Installation: Mounts the WordPress installation directory specified in the .env file into the Docker container.
  • Database Copy & URL Replacement:
    • Automatically extracts the database credentials from your wp-config.php or import the custom dump.sql file inside the import directory (placed manually).
    • Creates a copy of your local WordPress database (or use the provided dump file) and imports it into the Docker MySQL instance.
    • Uses the Search Replace DB script to replace the local URL (specified in WORDPRESS_LOCAL_URL in the .env file) with the dynamic URL generated by Cloudflare Tunnel.

Important Note

Make sure that the two variables in the .env file WORDPRESS_MYSQL_HOST and WORDPRESS_MYSQL_PORT are correct and that the MySQL server is reachable, or alternatively, manually place the previously exported dump.sql file into the import folder.

Local vs. Online Access

  • Local Access: Your WordPress site is accessible only from your local machine at the URL specified in your .env file, for example: https://www.example.local.
  • Online Access: Once the script is run, Cloudflare Tunnel will provide a dynamic public URL that exposes your site online, such as: https://dynamic-random-string.trycloudflare.com. This URL allows anyone to access your WordPress site from anywhere on the internet.

Getting Started (how to use)

  1. Clone the Repository:
git clone https://github.com/alfiosalanitri/wordpress-local-tunnel.git #or download the package zip from latest release

cd wordpress-local-tunnel
  1. Configure Your Environment:

Copy the .env.example file to .env and modify it according to your setup:

cp .env.example .env

Edit the .env file to set the following configurations:

  • COMPOSE_PROJECT_NAME: Prefix for Docker container names (e.g., example_preview).
  • WORDPRESS_LOCAL_URL: The local URL of your WordPress site (e.g., https://www.example.local).
  • WORDPRESS_INSTALL_DIR: Path to your local WordPress installation directory (e.g., /var/www/www.example.local/public_html).
  • WORDPRESS_MYSQL_HOST: Local MySQL server host from which to export the WordPress installation dump.
  • WORDPRESS_MYSQL_PORT: Local MySQL server port from which to export the WordPress installation dump.
  • DB_DUMP_ARGS: Custom dump export options like --column-statistics=0
  • WORDPRESS_DEBUG: true or false to enable or disable WORDPRESS_DEBUG
  • WORDPRESS_DEBUG_LOG: true or false to enable or disable WORDPRESS_DEBUG_LOG
  • WORDPRESS_DEBUG_DISPLAY: true or false to enable or disable WORDPRESS_DEBUG_DISPLAY
  • WEB_DOCKER_PORT: Port that exposes Apache in Docker (e.g., 8081).
  • MYSQL_DOCKER_PORT: Port for MySQL in Docker (e.g., 3310).
  • MYSQL_DOCKER_ROOT_USER: Root user for the MySQL instance (e.g., root).
  • MYSQL_DOCKER_ROOT_PASSWORD: Root password for the MySQL instance (e.g., root).
  • MYSQL_DOCKER_DATABASE: Name of the database used in Docker (e.g., ${COMPOSE_PROJECT_NAME}).
  1. Database (optional):

Place the dump.sql file into the import folder if you prefer or if the automatic dump from the first step fails for any reason.

  1. Start the Tunnel:
./go.sh

This will:

  • Export your local WordPress database (or use the provided dump file inside import directory) and import it into the Docker MySQL instance.
  • Create a backup of the wp-config.php file.
  • Create the wp-config.php file with Docker credentials.
  • Start MySQL, Apache, and PHP using Docker.
  • Mount your WordPress directory into the Docker container.
  • Run cloudflared to expose your local WordPress site online.
  • Replace the old url WORDPRESS_LOCAL_URL with the new Cloudflare url https://...trycloudflare.com
  1. Visit the Site:

The script will output a URL provided by Cloudflare, such as https://dynamic-random-string.trycloudflare.com, which you can use to access your WordPress site from anywhere.

Stopping the Tunnel and clear all

To stop the running Docker containers and the Cloudflare Tunnel, use:

./go.sh --stop

Troubleshooting

  • Port Conflicts: Make sure the ports used in the Docker containers (e.g., 3310 for MySQL, 8081 for Apache) are not in use by other services on your machine.
  • Cloudflare Errors: If you encounter issues with Cloudflare, ensure you have a stable internet connection and that Cloudflare's service is not down.
  • Database dump issue: If the first step database_dump fails because it cannot connect to the database for the automatic dump, make sure that the host WORDPRESS_MYSQL_HOST and the user saved in wp-config.php are allowed remote access. On the host machine that houses the Wordpress database, access MySQL and create the user if not already present. Example:
    # replace <val> with the correct values
    CREATE USER '<DB_USER>'@'<WORDPRESS_MYSQL_HOST>' IDENTIFIED WITH mysql_native_password BY '<DB_PASSWORD>';
    # grant permissions to all or a specific database if preferred
    GRANT ALL ON *.* TO '<DB_USER>'@'<WORDPRESS_MYSQL_HOST>';
    
    Alternatively, export the database manually and copy it to the import folder.

Contributing

Contributions are welcome! Feel free to submit issues, fork the repository, and create pull requests.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

  • Thanks to Cloudflare for providing the tunneling service.
  • This project uses Docker to manage the local WordPress environment.
  • Special thanks to Search Replace DB for the URL replacement script used in this project.

Tested Environments

This script has been tested in a Linux environment. Compatibility with other operating systems may vary.

About

This repository contains a Docker project that allows you to expose your locally WordPress site online using Cloudflare Tunnel via TryCloudflare. Useful when you want to share a preview of the site under development with a client.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published