Deploy laravel project on Cpanel

How to Deploy Laravel Project on Cpanel

Deploying a Laravel project on cPanel involves several steps. First, you prepare your Laravel application by configuring the environment and ensuring all dependencies are installed. Next, you upload your project files to the cPanel server using tools like File Manager or FTP. After that, you set up the database, update the .env file with correct settings, and configure cPanel for your application.

cPanel is a user-friendly hosting platform that simplifies web application deployment. It provides tools to manage files, databases, and server settings without technical expertise. For Laravel projects, cPanel ensures quick setup of PHP, MySQL, and required extensions. With its accessible interface, you can deploy projects efficiently and manage hosting tasks with ease.

Preparing the Laravel Project

Preparing your Laravel project is an essential step before deploying it on cPanel. Start by ensuring all dependencies are installed using Composer. Open your project directory and run composer install to fetch all required packages. This step ensures your application has the necessary libraries to function properly.

Next, configure the .env file in your project. This file holds crucial environment settings like database credentials, app URLs, and mail server details. Update these settings to match the configuration of your cPanel hosting environment. Ensure the database name, username, and password align with the details of the database you will create on cPanel.

Once your .env file is ready, optimize your project for deployment. Use commands like php artisan config:cache and php artisan route:cache to improve performance by caching configuration and route files. This step is vital to make your application run faster in a live environment.

Uploading Laravel Project Files to cPanel

1. Compressing Your Project Files

Before uploading, compress your Laravel project folder into a .zip file. This reduces upload time and simplifies file management.

2. Using cPanel File Manager

Log in to your cPanel account and navigate to the File Manager. Go to the target directory, usually public_html, and upload the .zip file. After the upload, use the Extract option to unpack the files.

3. Using FTP for File Transfer

Alternatively, you can use an FTP client like FileZilla. Connect to your server with your cPanel credentials and transfer the project files to the appropriate directory. Ensure hidden files like .env and .htaccess are included in the upload.

4. Setting the Public Directory as Root

Move the contents of the public folder in your Laravel project to the public_html directory. Update file paths in the index.php file to reflect the new location. This step ensures your application is accessible via a browser.

Setting Up Database on cPanel

Compressing Your Project Files

Before uploading, compress your Laravel project folder into a .zip file. This reduces upload time and simplifies file management.

Using cPanel File Manager

Log in to your cPanel account and navigate to the File Manager. Go to the target directory, usually public_html, and upload the .zip file. After the upload, use the Extract option to unpack the files.

Using FTP for File Transfer

Alternatively, you can use an FTP client like FileZilla. Connect to your server with your cPanel credentials and transfer the project files to the appropriate directory. Ensure hidden files like .env and .htaccess are included in the upload.

Setting the Public Directory as Root

Move the contents of the public folder in your Laravel project to the public_html directory. Update file paths in the index.php file to reflect the new location. This step ensures your application is accessible via a browser.

Configuring cPanel for Laravel

To deploy a Laravel project on cPanel, you must ensure the server is configured correctly. Begin by setting up the PHP version required by your Laravel project. Log in to cPanel and navigate to Select PHP Version under the Software section. Choose the recommended PHP version (such as 8.0 or higher) and click Set as Current to apply the changes. This ensures your project is compatible with the hosting environment.

Next, enable the required PHP extensions for Laravel. These typically include PDO, openssl, mbstring, tokenizer, and others specified in Laravel’s documentation. You can enable these extensions in the same Select PHP Version interface. Scroll through the list, check the relevant extensions, and save the configuration. Without these extensions, Laravel won’t function as expected.

You may also need to adjust PHP settings to match Laravel’s requirements. Using the MultiPHP INI Editor, increase limits like memory_limit, upload_max_filesize, and post_max_size. These adjustments allow Laravel to handle larger files and processes smoothly. Ensure your server’s configuration matches the demands of your application.

Setting Permissions for Storage and Cache

Proper permissions for the storage and bootstrap/cache directories are essential for your Laravel project to work correctly. These directories store critical files, such as logs, compiled views, and cached data. Without the correct permissions, Laravel cannot read or write to these directories, leading to errors.

Start by logging in to your cPanel account and opening the File Manager. Navigate to the storage and bootstrap/cache directories. Right-click on each directory, select Permissions, and set the permissions to 755 or 775, depending on your hosting provider’s requirements. This allows Laravel to write to these directories while maintaining security.

If you encounter permission errors, you can adjust them using the terminal or SSH access. Run the command chmod -R 775 storage bootstrap/cache to set the appropriate permissions. Additionally, ensure the directories are owned by the correct user by running chown -R yourusername:yourgroup storage bootstrap/cache.

Updating the .htaccess File

The .htaccess file is critical for configuring your Laravel project on cPanel. It manages important tasks like redirecting traffic, ensuring proper routing, and blocking access to sensitive files. After uploading your Laravel project, locate the .htaccess file in the public directory using the cPanel File Manager. Make sure hidden files are visible by enabling the Show Hidden Files option.

Review the file’s content to verify it contains the necessary rules to redirect all traffic to Laravel’s index.php. This ensures proper handling of routes and prevents unauthorized access to directories. If your application encounters routing or access issues, update the file with the correct rules as specified in Laravel’s documentation.

After making changes, save the file and test your Laravel application to confirm everything works as expected. Proper configuration of the .htaccess file ensures seamless navigation and enhances the security of your project.

Running Artisan Commands on cPanel

Artisan commands are vital for managing and optimizing your Laravel project. On cPanel, you can run these commands using the terminal or SSH access. Begin by logging in to your cPanel account and navigating to the Terminal or enabling SSH access if it’s not already active.

Once inside the terminal, move to your Laravel project directory, typically located in public_html or a subdirectory. Use the cd command to navigate to the directory where your project files are stored. From here, you can execute Artisan commands such as clearing caches, migrating databases, or creating symbolic links. For example, you may need to run commands like php artisan config:cache or php artisan migrate to set up or optimize your project.

Ensure that permissions and paths are configured correctly to avoid errors while running commands. Testing the results of each command is important to confirm proper execution. Running Artisan commands directly from cPanel provides flexibility and control over your Laravel project’s management and maintenance.

Testing and Debugging the Deployment

1. Initial Testing in the Browser

Access your application via your domain or subdomain to verify the deployment. Check if all pages load correctly, ensuring routes, links, and assets function as expected. Identify any visible errors or missing elements during navigation.

2. Enabling Debug Mode

If issues occur, enable Laravel’s debug mode. Open the .env file and set APP_DEBUG to true. This will display detailed error messages on the browser, making it easier to identify and resolve problems. Don’t forget to disable debug mode after testing to maintain security.

3. Reviewing Log Files

Check Laravel’s logs in the storage/logs directory for detailed error reports. Additionally, use cPanel’s Error Log tool to identify server-side issues, such as PHP errors or configuration problems.

4. Resolving and Retesting

Fix the identified errors, such as incorrect file permissions, missing dependencies, or misconfigured environment variables. Retest your application after making changes to ensure all issues are resolved and the deployment is successful.

Conclusion

Deploying a Laravel project on cPanel requires careful steps to ensure everything works smoothly. From preparing your project and uploading files to configuring settings and testing, each step plays a key role. Following these steps ensures your application runs efficiently and securely on the server.

Take your time to check all configurations, permissions, and routes during deployment. If errors occur, use Laravel’s debug mode and log files to fix them. With the right approach, deploying your Laravel project on cPanel becomes a straightforward process, providing a reliable environment for your application to operate effectively.

Share the article

Written By

Author Avatar

January 17, 2025

Ayesha Khan is a highly skilled technical content writer based in Pakistan, known for her ability to simplify complex technical concepts into easily understandable content. With a strong foundation in computer science and years of experience in writing for diverse industries, Ayesha delivers content that not only educates but also engages readers.

Leave a Reply

Your email address will not be published. Required fields are marked *