Taking Your Laravel Website Live: A Beginner's Guide to Deploying on cPanel and hPanel
Taking Your Laravel Website Live: A Beginner's Guide to Deploying on cPanel and hPanel

Taking Your Laravel Website Live: A Beginner’s Guide to Deploying on cPanel and hPanel21 min read

  Reading time 30 minutes

Laravel Website: You’ve built an amazing web application using Laravel on your local machine. It works perfectly in your development environment. But now comes the exciting part: sharing it with the world! Getting your website from localhost onto a live server can feel a bit daunting, especially if it’s your first time.

Fear not, aspiring web developer! This comprehensive guide will walk you through the process step-by-step, focusing on two of the most common control panels used by shared hosting providers: cPanel and Hostinger’s hPanel. We’ll break down each stage, explaining what you need to do and why, making the deployment process as smooth as possible for beginners.

By the end of this post, you’ll have a clear understanding of how to move your Laravel project from your computer to a live server, making your creation accessible to anyone with an internet connection.

Let’s dive in!

Why Laravel Website Deployment Matters

Building locally is great for development and testing, but for others to see and use your application, it needs to live on a web server connected to the internet. Deployment is the process of packaging your application files, setting up the necessary environment on the server, and making your website accessible via a domain name.

For Laravel applications, this involves more than just uploading files. You need to configure the server, set up a database, handle dependencies, and ensure your application can run correctly in a different environment than your local setup.

Prerequisites: What You Need Before You Start Laravel Website

Before we begin the deployment process, make sure you have the following ready:

  1. Your Completed Laravel Project: All your code, assets, and configurations should be finalized (or at least in a stable state you want to deploy).
  2. A Hosting Account: You’ll need a web hosting account with a provider that offers either cPanel or hPanel access. Ensure your hosting plan supports PHP and has database capabilities (usually MySQL).
  3. Domain Name: A registered domain name pointing to your hosting server. This is how users will access your website.
  4. FTP/SFTP Client (Optional but Recommended): Software like FileZilla, Cyberduck, or WinSCP can make transferring files much easier than using the web-based File Manager, especially for larger projects.
  5. Database Management Tool (Optional): Tools like MySQL Workbench, DBeaver, or TablePlus can help manage your local database and export it easily. phpMyAdmin, available via cPanel and hPanel, is also an option.
  6. Terminal/Command Prompt: For running local commands (like database export). SSH access to your server (if your hosting plan provides it) will be incredibly helpful later on.

Part 1: Preparing Your Laravel Website for Deployment

Before you upload anything, there are a few crucial steps to take on your local machine to prepare your Laravel application.

Step 1: Export Your Database

Your application likely stores data in a database. You need to export a copy of your local database so you can import it onto the live server.

  • Using phpMyAdmin (Common with XAMPP/WAMP/MAMP):
    • Go to your local phpMyAdmin instance (usually http://localhost/phpmyadmin).
    • Select the database for your Laravel project from the left sidebar.
    • Click the “Export” tab.
    • Choose the “Custom” export method if you want more control, but for a simple export, the “Quick” method is usually fine.
    • Ensure the format is set to “SQL”.
    • Click “Go”. This will download a .sql file containing all your database tables and data.
  • Using a Command Line Tool:
    • Open your terminal or command prompt.
    • Use the mysqldump command. The syntax is generally:
mysqldump -u your_database_username -p your_database_name > export_file_name.sql

  • Replace your_database_username and your_database_name with your local database credentials. You’ll be prompted to enter your password.
  • This will create an .sql file in your current directory.

Important: Save this .sql file in a safe place. You’ll need it for importing into your live server database.

Step 2: Understand Composer Dependencies

When you run composer install locally, Composer reads your composer.json file and downloads all the required libraries and packages into the vendor directory.

For deployment, it’s generally not recommended to upload your entire vendor directory. This is because server environments can differ slightly, and it’s best to let Composer install dependencies directly on the server based on its specific environment.

However, with shared hosting, you often don’t have direct SSH access to easily run Composer commands. We’ll address this limitation in the deployment steps.

Step 3: Consider Your .env File

The .env file contains sensitive configuration information, such as database credentials, API keys, and application settings.

  • Security: Never commit your .env file to version control (like Git) if it’s publicly accessible.
  • Server Specifics: The .env file on your live server will be different from your local one, primarily due to database credentials (DB_DATABASE, DB_USERNAME, DB_PASSWORD, DB_HOST) and the application URL (APP_URL).
  • Creation: You will manually create or edit the .env file on your live server after uploading your project files.

Step 4: Review Your Project Structure (Laravel Website)

Familiarize yourself with the standard Laravel project structure. The most critical directory for web server configuration is the public directory. This directory contains the index.php file, which is the entry point for all requests to your Laravel application. The web server on your host must be configured to point to this public directory for your application to work correctly.

Part 2: Deploying to cPanel (Laravel Website)

cPanel is a popular web hosting control panel known for its user-friendly interface. Here’s how to deploy your Laravel application using cPanel.

Step 1: Access Your cPanel Account (Laravel Website)

Log in to your web hosting account and navigate to your cPanel dashboard. The URL is usually yourdomain.com/cpanel or provided in your hosting welcome email.

Step 2: Create a Database and Database User (Laravel Website)

Your Laravel application needs a database on the server.

Create a Database and Database User Laravel Website
Create a Database and Database User (Laravel Website)
  1. Find the MySQL® Databases Icon: In cPanel, look for the “Databases” section and click on “MySQL® Databases”.
  2. Create a New Database: Under “Create New Database”, enter a name for your database (e.g., yourprefix_mydatabase). cPanel automatically adds a prefix based on your account username. Click “Create Database”.
  3. Create a New User: Scroll down to “Add New User”. Enter a username and a strong password. Click “Create User”. Make a note of the full database name, username, and password you just created.
  4. Add User to Database: Scroll down to “Add User To Database”. Select the user you just created from the “User” dropdown and the database you just created from the “Database” dropdown. Click “Add”.
  5. Manage User Privileges: On the next screen, select “ALL PRIVILEGES” and click “Make Changes”. This grants the user full access to the database, which is necessary for your application.

Step 3: Upload Your Laravel Project Files

You have two main options for uploading files:

  • Using cPanel File Manager:
    1. In cPanel, find the “Files” section and click on “File Manager”.
    2. Navigate to the directory where you want to upload your files. For your primary domain, this is typically public_html. If deploying to a subdomain or addon domain, navigate to the corresponding directory.
    3. Important: You need to upload the contents of your Laravel project directory excluding the vendor and potentially node_modules (if you use NPM for frontend assets) directories. The best practice is to compress your project locally into a .zip file without the vendor folder, then upload the .zip file.
    4. Click the “Upload” button at the top.
    5. Drag and drop your .zip file or use the “Select File” button to upload it.
    6. Once uploaded, right-click the .zip file in File Manager and select “Extract”. Choose the destination directory (e.g., public_html). This will extract your project files.
    7. After extraction, you can delete the .zip file to save space.
  • Using an FTP/SFTP Client (Recommended):
    1. Get your FTP/SFTP credentials from your hosting provider or cPanel (usually under “FTP Accounts”). You’ll need the server address, username, password, and port (usually 21 for FTP, 22 for SFTP).
    2. Open your FTP/SFTP client and connect to your server.
    3. Navigate to the target directory on the server (public_html or your domain’s directory).
    4. On your local computer, navigate to your Laravel project directory.
    5. Crucially, do NOT upload the vendor directory. Select all other files and folders (like app, bootstrap, config, database, resources, routes, storage, tests, .env.example, artisan, composer.json, composer.lock, etc.) and upload them to the target directory on the server. This process can take some time depending on your project size and internet speed.

Step 4: Configure the .env File

Now that your files are uploaded, you need to create or edit the .env file on the server with the live server’s details.

  1. Locate the .env file: In cPanel File Manager (or your FTP/SFTP client), navigate to the root of your Laravel project directory (e.g., public_html). Look for a file named .env.example.
  2. Rename and Edit: Rename .env.example to .env. If you already uploaded a .env file from local (less ideal, but possible), just edit that one.
  3. Edit the .env file: Right-click the .env file and select “Edit” (or use your FTP client’s edit function).
  4. Update Database Credentials: Modify the DB_ variables to match the database name, username, and password you created in Step 2:
DB_CONNECTION=mysql
DB_HOST=localhost # Often 'localhost', but check your host's docs if issues arise
DB_PORT=3306
DB_DATABASE=yourprefix_mydatabase_name
DB_USERNAME=yourprefix_database_user
DB_PASSWORD=YourStrongPassword

5. Update App URL: Set APP_URL to your domain name:

APP_URL=http://yourdomain.com

(Use https if you have an SSL certificate installed).

6. Set APP_DEBUG: For a live site, it’s highly recommended to set APP_DEBUG=false for security and performance reasons.

7. Save Changes: Save the .env file.

Step 5: Import Your Database

Now, import the .sql file you exported earlier into the database you created on the server.

  1. Find phpMyAdmin: In cPanel, go back to the “Databases” section and click on “phpMyAdmin”.
  2. Select Your Database: In the left sidebar of phpMyAdmin, click on the database name you created for your project (e.g., yourprefix_mydatabase).
  3. Click the “Import” Tab: At the top of the phpMyAdmin interface, click the “Import” tab.
  4. Choose File: Click the “Choose File” button and select the .sql file you exported from your local database.
  5. Go: Scroll down and click the “Go” button. phpMyAdmin will execute the SQL commands in the file, creating your tables and inserting your data. You should see a success message if everything goes well.

Step 6: Point Your Web Server to the public Directory

This is a critical step. Your web server (Apache or Nginx, typically managed by cPanel) needs to know that the entry point for your application is the index.php file inside the public directory, not the root of your project.

With cPanel, you can often achieve this by modifying the domain’s configuration or using a .htaccess file.

  • Method 1: Modifying Document Root (If available and recommended) Some hosting providers allow you to change the “Document Root” for your domain or subdomain to point directly to the public subdirectory within your project folder. This is the cleanest approach. Look for options related to “Domains,” “Subdomains,” or “Addon Domains” in cPanel and see if you can edit the path. Change it from something like /home/youruser/public_html to /home/youruser/public_html/your_project_folder/public.
  • Method 2: Using a .htaccess Redirect (More common for shared hosting without Document Root changes) If you cannot change the Document Root, you can use an .htaccess file in your main public_html directory (or the root of your domain/subdomain) to redirect all requests to the public subdirectory.
    1. In cPanel File Manager, navigate to your main public_html directory (or the root directory for your domain).
    2. Click “+ File” to create a new file. Name it .htaccess (ensure you include the dot at the beginning).
    3. Edit the .htaccess file and add the following code:
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^(.*)$ your_project_folder/public/$1 [L]
</IfModule>

    • Explanation:
      • <IfModule mod_rewrite.c>: Checks if the Apache rewrite module is enabled.
      • RewriteEngine On: Turns on the rewrite engine.
      • RewriteRule ^(.*)$ your_project_folder/public/$1 [L]: This is the core rule. It captures any request (^(.*)$) and silently rewrites it to point to the same path inside your_project_folder/public/. Replace your_project_folder with the actual name of the folder where you uploaded your Laravel files (e.g., laravel_project). The [L] flag means this is the last rule to apply.
    • Important: Make sure your_project_folder is the correct name of the directory containing your Laravel project files within public_html.

Step 7: Handle Dependencies and Other Commands (If No SSH)

Shared hosting often limits SSH access, making it difficult to run composer install, php artisan migrate, etc. Here are a few workarounds:

  • Composer Install:
    • Option A (Less Ideal but sometimes necessary): Upload the vendor folder from your local machine. This isn’t best practice due to potential environment differences, but if you have no other way to run Composer, it might work.
    • Option B (Better): Some hosting providers offer a “Terminal” or “SSH” feature within cPanel, or a separate web-based terminal. If you have access to any kind of command-line interface, navigate to your project root and try running composer install.
    • Option C (Advanced): If you have dedicated hosting or a VPS, SSH is standard, and you would simply cd into your project directory and run composer install.
  • Running Artisan Commands (php artisan migrate, php artisan key:generate, etc.):
    • Similar to Composer, if you have any command-line access (SSH, web terminal), you can run these commands directly.
    • php artisan key:generate: This generates your APP_KEY in the .env file, which is crucial for security features like session encryption. If you cannot run this command, you must manually add a 32-character random string as the APP_KEY in your .env file. You can generate one locally with php artisan key:generate and copy it.
    • php artisan migrate: Runs your database migrations. If you can’t run this, you’ll need to import your database structure along with data in Step 5, ensuring your local database is up-to-date before exporting.
    • php artisan storage:link: Creates a symbolic link from public/storage to storage/app/public. This is needed to display files uploaded via Storage::disk('public'). If you can’t run commands, you might need to manually copy files from storage/app/public to a publicly accessible directory or explore alternative storage methods.

Step 8: Set Directory and File Permissions

Incorrect file permissions are a common issue. Certain directories need write permissions for Laravel to function correctly.

  • Required Permissions: The storage and bootstrap/cache directories need to be writable by the web server user.
  • Using cPanel File Manager:
    1. Right-click on the storage folder within your project root.
    2. Select “Change Permissions”.
    3. Set the permissions to 755 (or 775 or 777 if 755 doesn’t work, but 777 is less secure). Ensure “Apply changes to files and subfolders” is checked.
    4. Repeat the process for the bootstrap/cache folder.
  • Using FTP/SFTP Client: Most FTP clients allow you to change permissions by right-clicking files/folders and selecting “File permissions…” or “Change permissions” (often called CHMOD). Set the numeric value to 755 or 775 for directories and 644 or 664 for files. Apply recursively where needed.

Step 9: Clear Cache

Laravel caches configurations and views to improve performance. After deployment, it’s a good idea to clear these caches.

  • If you have command-line access (SSH/Web Terminal):
php artisan optimize:clear

or individually:

php artisan cache:clear
php artisan config:clear
php artisan route:clear
php artisan view:clear

  • If you don’t have command-line access: This is tricky. You might need to temporarily create a route in your web.php file that runs these commands when visited, and then immediately remove that route after visiting it for security reasons.
// Temporarily add this route (DANGER: Remove after use!)
Route::get('/clear-cache', function() {
    Artisan::call('cache:clear');
    Artisan::call('config:clear');
    Artisan::call('route:clear');
    Artisan::call('view:clear');
    return "Cache cleared!";
});

  • Visit yourdomain.com/clear-cache once, then immediately remove this route from web.php and re-upload the file.

Step 10: Test Your Website

Open your web browser and navigate to your domain name. If everything is configured correctly, you should see your Laravel application running! Test different pages and functionalities to ensure everything works as expected.

Part 3: Deploying to hPanel (Hostinger)

Hostinger uses its own custom control panel called hPanel. The steps are very similar to cPanel, with slight differences in interface and terminology.

Step 1: Access Your hPanel Account

Log in to your Hostinger account and select the website you want to manage. This will take you to the hPanel dashboard for that site.

Step 2: Create a Database and Database User

  1. Find Databases: In the hPanel sidebar, look for the “Databases” section and click on “Management”.
  2. Create a New Database: Under “Create New MySQL Database and Database User”, enter the desired database name, username, and a strong password. Make a note of these details. Click “Create”. hPanel automatically links the user to the database.

Step 3: Upload Your Laravel Project Files

Similar to cPanel, you can use hPanel’s File Manager or an FTP/SFTP client.

  • Using hPanel File Manager:
    1. In hPanel, find the “Files” section and click on “File Manager”. This will open a new tab with the web-based file manager.
    2. Navigate to the directory for your domain. This is usually domains/yourdomain.com.
    3. Important: As with cPanel, upload the contents of your Laravel project directory excluding vendor and node_modules. Compress your project locally into a .zip file without the vendor folder.
    4. Click the “Upload” icon (usually a cloud with an arrow) at the top. Select “File” and choose your .zip file.
    5. Once uploaded, right-click the .zip file and select “Extract”. Enter the destination directory (e.g., domains/yourdomain.com).
    6. After extraction, delete the .zip file.
  • Using an FTP/SFTP Client:
    1. Get your FTP/SFTP credentials from hPanel (usually under “Files” > “FTP Accounts”).
    2. Connect using your FTP/SFTP client.
    3. Navigate to your domain’s directory on the server (e.g., domains/yourdomain.com).
    4. Upload all files and folders from your local Laravel project except the vendor directory.

Step 4: Configure the .env File

  1. Locate the .env file: In hPanel File Manager (or your FTP client), navigate to the root of your Laravel project directory (e.g., domains/yourdomain.com/your_project_folder). Look for .env.example.
  2. Rename and Edit: Rename .env.example to .env.
  3. Edit the .env file: Edit the .env file and update the DB_ variables with the database name, username, and password you created in Step 2.
DB_CONNECTION=mysql
DB_HOST=localhost # Usually 'localhost' on Hostinger
DB_PORT=3306
DB_DATABASE=uXXXX_yourdatabase
DB_USERNAME=uXXXX_youruser
DB_PASSWORD=YourStrongPassword

(Hostinger database names and usernames often start with u followed by a number).

4. Update App URL: Set APP_URL to your domain name:

APP_URL=http://yourdomain.com

  1. (Use https if you have an SSL certificate installed).
  2. Set APP_DEBUG: Set APP_DEBUG=false.
  3. Save Changes: Save the .env file.

Step 5: Import Your Database

  1. Find phpMyAdmin: In hPanel, go back to “Databases” > “Management” and click the “phpMyAdmin” button next to your database.
  2. Select Your Database: In the left sidebar of phpMyAdmin, click on your database name.
  3. Click the “Import” Tab: Click the “Import” tab at the top.
  4. Choose File: Click “Choose File” and select your exported .sql file.
  5. Go: Scroll down and click “Go”.

Step 6: Point Your Web Server to the public Directory (Hostinger Specific)

Hostinger has a specific setting to define the web root for your domain.

  1. Find “Set webiste root”: In hPanel, navigate to the “Website” section and click on “Set website root”.
  2. Select the public folder: Click the “Select folder” button. Navigate through your file structure (domains/yourdomain.com/your_project_folder) and select the public folder.
  3. Confirm: Click the “Select this folder” button. This tells Hostinger’s web server to serve files from within that public directory.

Step 7: Handle Dependencies and Other Commands (If No SSH on Hostinger)

Hostinger offers an “SSH Access” feature under the “Advanced” section. If your plan includes it, you can use it to run Composer and Artisan commands.

  1. Enable SSH: Go to “Advanced” > “SSH Access” and enable it. You’ll get connection details (hostname, port, username, password).
  2. Connect via SSH Client: Use an SSH client (like PuTTY on Windows, or the built-in Terminal on macOS/Linux) to connect using the provided details.
  3. Navigate to Project: Once connected, navigate to your Laravel project root directory (e.g., cd domains/yourdomain.com/your_project_folder).
  4. Run Commands:
    • composer install: Download and install dependencies.
    • php artisan key:generate: Generate your application key.
    • php artisan migrate: Run migrations.
    • php artisan storage:link: Create the storage link.
    • php artisan optimize:clear: Clear caches.
  • If you DON’T have SSH access: You face the same limitations as with cPanel. You might need to upload the vendor folder (less ideal), manually generate the APP_KEY, and use the temporary route trick for clearing caches.

Step 8: Set Directory and File Permissions

Use the hPanel File Manager or your FTP/SFTP client to set permissions for storage and bootstrap/cache.

  • Using hPanel File Manager: Right-click on the storage and bootstrap/cache folders, select “Permissions”, and set them to 755 (or 775). Apply recursively.

Step 9: Clear Cache

If you have SSH access, use php artisan optimize:clear. If not, use the temporary route method described in the cPanel section.

Step 10: Test Your Website

Visit your domain name in a browser and verify that your Laravel application is running correctly.

Part 4: Post-Deployment & Troubleshooting

Even with careful steps, you might encounter issues. Here are some common problems and how to fix them:

  • “Whoops, looks like something went wrong.” or White Screen:
    • Check Server Error Logs: Look for “Error Logs” in cPanel or hPanel. This is the best place to find detailed error messages.
    • Set APP_DEBUG=true Temporarily: In your .env file, set APP_DEBUG=true. This will display the actual Laravel error message in your browser. REMEMBER TO SET IT BACK TO false AFTER DEBUGGING!
  • Permissions Issues: If you see errors related to files or directories not being writable, double-check the permissions of the storage and bootstrap/cache directories (should be 755 or 775).
  • Database Connection Errors: Verify the database credentials (DB_DATABASE, DB_USERNAME, DB_PASSWORD, DB_HOST) in your .env file exactly match the database and user you created on the server.
  • APP_KEY Not Set: If you see errors about encryption keys, ensure APP_KEY is set in your .env file. It should be a 32-character random string. Generate one locally with php artisan key:generate if needed.
  • Files Not Found (404 Errors):
    • Ensure your web server is pointing to the public directory (Step 6 in both sections).
    • Check that you uploaded all necessary files and folders (excluding vendor).
    • Verify your .htaccess file (if using that method) is correctly placed and configured.
  • Missing Assets (CSS, JS, Images):
    • Ensure you uploaded your frontend assets.
    • Check that paths in your code pointing to assets are correct (e.g., using Laravel’s asset() helper).
    • If using php artisan storage:link, ensure the symbolic link was created correctly (often requires SSH) or manually copy files from storage/app/public to public/storage.
  • Composer Dependencies Missing: If you uploaded without the vendor folder and couldn’t run composer install, you’ll get errors about classes not found. You’ll need to find a way to run composer install on the server or upload the vendor folder (as a last resort).

Conclusion: Your Laravel Site is Live!

Congratulations! You’ve successfully deployed your Laravel website from your local development environment to a live server using either cPanel or hPanel. This is a significant step in your journey as a web developer.

Remember that deployment can sometimes be tricky, and encountering errors is a normal part of the process. By understanding the steps involved, knowing where to check for errors, and patiently troubleshooting, you can overcome most challenges.

Keep refining your application and deploying updates. The more you practice, the smoother the process will become. Now, go share your creation with the world!


Citation URL (for general Laravel information): https://laravel.com/docs

Citation URL (for general cPanel information): https://docs.cpanel.net/

Citation URL (for general Hostinger information): https://www.hostinger.com/tutorials/

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply