Streamlining CPanel Deployments: A Guide to Automated Deployment Scripts and .htaccess

Project Context: 11-once-landing Deployment

In the 11-once-landing project, our focus recently shifted to optimizing and standardizing our deployment process. Specifically, we tackled the challenge of deploying applications to a CPanel environment, which often requires a more hands-on approach compared to modern CI/CD pipelines. This involved setting up configuration for CPanel, defining .htaccess rules for Apache, and creating a robust deploy:main script to automate the entire process.

The Need for Automated CPanel Deployment

CPanel, while offering a user-friendly interface for managing hosting, can present challenges for continuous deployment. Manual file transfers and configuration updates are time-consuming and prone to human error. Our goal was to create a repeatable and reliable deployment mechanism that allows for quick updates to our 11-once-landing application.

Understanding CPanel Hosting

CPanel environments typically serve web applications from a public_html directory. Updates usually involve uploading new files via FTP/SFTP or Git. We opted for a Git-based approach combined with a shell script, allowing us to push changes to a repository and then trigger a deployment directly on the server.

Crafting the deploy:main Script

The core of our automated CPanel deployment is a simple yet powerful shell script. This script executes a series of commands designed to fetch the latest code, build the application (if necessary), and then move the compiled assets into the correct public_html directory. This ensures that every deployment is consistent and minimizes downtime.

Here’s a generic example of what such a deploy.sh script might look like:

#!/bin/bash

PROJECT_ROOT="/home/youruser/repo-name"
PUBLIC_DIR="/home/youruser/public_html"

# Navigate to the project root
cd "$PROJECT_ROOT"

# Pull the latest changes from the main branch
echo "Pulling latest code from Git..."
git pull origin main

# Install dependencies (if applicable, e.g., for Node.js projects)
echo "Installing dependencies..."
npm install

# Build the project (if applicable, e.g., for frontend frameworks)
echo "Building project..."
npm run build

# Clear existing public_html contents (use with caution!)
echo "Clearing public_html..."
rm -rf "$PUBLIC_DIR/*"

# Copy built assets to the public_html directory
echo "Copying built assets to public_html..."
cp -r "$PROJECT_ROOT/dist/." "$PUBLIC_DIR/"

echo "Deployment complete!"

This script first navigates to the project's repository, pulls the latest code, installs dependencies, builds the project, clears the public_html directory (to ensure a clean slate), and then copies the newly built application files. This process can be triggered manually via SSH or set up as a Git hook for true automation.

Configuring Apache with .htaccess

For many web applications, especially Single Page Applications (SPAs), Apache's .htaccess file is crucial for handling URL rewrites and routing. In a CPanel environment, placing an .htaccess file in your public_html directory allows you to override default server configurations without direct access to httpd.conf.

For instance, an SPA typically needs all non-file/directory requests to be routed to index.html. Here's a common .htaccess configuration:

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteRule ^index\.html$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . /index.html [L]
</IfModule>

# Optional: Set default charset
AddDefaultCharset UTF-8

# Optional: Cache control for static assets
<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresByType image/jpg "access 1 year"
  ExpiresByType image/jpeg "access 1 year"
  ExpiresByType image/gif "access 1 year"
  ExpiresByType image/png "access 1 year"
  ExpiresByType text/css "access 1 month"
  ExpiresByType application/javascript "access 1 month"
</IfModule>

This .htaccess ensures that your application handles routing, providing a clean user experience while leveraging Apache for performance optimizations like caching.

Conclusion

By combining a dedicated shell script for deployment and a well-configured .htaccess file, we've significantly improved the development and deployment workflow for the 11-once-landing project on CPanel. This approach moves beyond manual file transfers, providing a more robust, consistent, and less error-prone way to update web applications.

Actionable takeaway: Implement a simple deployment script for your CPanel projects. Even a basic script can save countless hours and prevent common deployment errors, making your workflow smoother and more professional. Start by automating git pull and file copy operations, and then gradually add build steps and dependency management as needed. Donn't forget to configure .htaccess for proper routing and caching on your Apache server.


Generated with Gitvlg.com

Streamlining CPanel Deployments: A Guide to Automated Deployment Scripts and .htaccess
Marco Carvallo

Marco Carvallo

Author

Share: