In our previous post, “Everything You Need To Know About wp-config.php File”, we recommended moving the wp-config.php file out of the public directory to enhance security for your WordPress installation.
However, it’s important to do this correctly to avoid either open_basedir errors or web app loading issues.
Make sure you follow the following steps to learn how to do this correctly.
For New WordPress Apps
If you are creating a new WordPress website, you can follow the steps described in How To Deploy A WordPress Website. However, as highlighted in the following screenshot, you need to specify a public directory (e.g., “public_html” or “public”) for your website just before deploying the web application.
After adding the public path, you can deploy the website. After only a couple of seconds, you should be able to browse this website in your browser normally like any other WordPress website.
However, at this point, your wp-config.php file will still be in the same directory as your website. You can move the wp-config.php file one level up – either from the RunCloud dashboard or via SSH.
Method 1: Moving wp-config.php Using the RunCloud File Manager
If you want to use the RunCloud dashboard, you’ll need to manually create a new file in the desired directory, and copy the contents of the old file using the RunCloud file manager.
After saving the new file, you must manually delete the old file for the changes to take effect.
Method 2: Moving wp-config.php via SSH
If you want to perform this action on multiple websites, copying and pasting manually can get pretty repetitive.
To avoid this, you can connect to your server via SSH and navigate to your WordPress installation directory. In that folder, just run the following command to move the wp-config.php file from the old location to the new location – not forgetting to replace “public_html” with the actual public folder name that you configured during the installation:
mv public_html/wp-config.php .
For Existing WordPress Apps
If you already have a WordPress website on your RunCloud server, you first need to log in to your RunCloud dashboard and navigate to your Web Application settings.
In the “Public Path” setting, change it to a subdirectory of your current path by adding /public_html (or any other path) at the end.
After saving the changes in the RunCloud dashboard, connect to your server via SSH and execute the commands below to perform the following actions:
- Navigate to your WordPress installation directory
- Move WordPress files to the new public directory
- Move wp-config.php back to the original directory
cd <path-to-wp>
mv ./* ./public_html/
mv ./public_html/wp-config.php .
In the above snippet, make sure to replace the <path-to-wp> with the path that is displayed in your RunCloud dashboard, and public_html with the path that you specified earlier.
Final Actions
By following the above steps, you can safely move your wp-config.php file out of the public path. However, you should perform some additional actions to avoid common errors and ensure your WordPress site continues to function correctly:
- open_basedir: If you encounter open_basedir errors, you may need to adjust the open_basedir setting in your PHP configuration to include the parent directory.
- File paths: Update any hardcoded file paths in your WordPress configuration or plugins to reflect the new directory structure.
- .htaccess: If you’re using Apache, you may need to update your .htaccess file to reflect the new directory structure.
- Cache: Clear any caches (WordPress, browser, and server-level) after making these changes to avoid unexpected errors.
- Edit wp-config.php: Finally, edit the wp-config.php file and add the following line, replacing “public_html” with the name of your public folder that you specified earlier:
define('ABSPATH', __DIR__ . '/public_html/');
That’s it! Your website should be up and running smoothly. If someone tries to visit the wp-config.php page directly in the browser, they should see the following error page: