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!
Table of Contents
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:
- Your Completed Laravel Project: All your code, assets, and configurations should be finalized (or at least in a stable state you want to deploy).
- 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).
- Domain Name: A registered domain name pointing to your hosting server. This is how users will access your website.
- 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.
- 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.
- 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
.sqlfile containing all your database tables and data.
- Go to your local phpMyAdmin instance (usually
- Using a Command Line Tool:
- Open your terminal or command prompt.
- Use the
mysqldumpcommand. The syntax is generally:
mysqldump -u your_database_username -p your_database_name > export_file_name.sql- Replace
your_database_usernameandyour_database_namewith your local database credentials. You’ll be prompted to enter your password. - This will create an
.sqlfile 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
.envfile to version control (like Git) if it’s publicly accessible. - Server Specifics: The
.envfile 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
.envfile 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.
- Find the MySQL® Databases Icon: In cPanel, look for the “Databases” section and click on “MySQL® Databases”.
- 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”. - 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.
- 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”.
- 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:
- In cPanel, find the “Files” section and click on “File Manager”.
- 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. - Important: You need to upload the contents of your Laravel project directory excluding the
vendorand potentiallynode_modules(if you use NPM for frontend assets) directories. The best practice is to compress your project locally into a.zipfile without thevendorfolder, then upload the.zipfile. - Click the “Upload” button at the top.
- Drag and drop your
.zipfile or use the “Select File” button to upload it. - Once uploaded, right-click the
.zipfile in File Manager and select “Extract”. Choose the destination directory (e.g.,public_html). This will extract your project files. - After extraction, you can delete the
.zipfile to save space.
- Using an FTP/SFTP Client (Recommended):
- 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).
- Open your FTP/SFTP client and connect to your server.
- Navigate to the target directory on the server (
public_htmlor your domain’s directory). - On your local computer, navigate to your Laravel project directory.
- Crucially, do NOT upload the
vendordirectory. Select all other files and folders (likeapp,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.
- Locate the
.envfile: 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. - Rename and Edit: Rename
.env.exampleto.env. If you already uploaded a.envfile from local (less ideal, but possible), just edit that one. - Edit the
.envfile: Right-click the.envfile and select “Edit” (or use your FTP client’s edit function). - 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=YourStrongPassword5. 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.
- Find phpMyAdmin: In cPanel, go back to the “Databases” section and click on “phpMyAdmin”.
- Select Your Database: In the left sidebar of phpMyAdmin, click on the database name you created for your project (e.g.,
yourprefix_mydatabase). - Click the “Import” Tab: At the top of the phpMyAdmin interface, click the “Import” tab.
- Choose File: Click the “Choose File” button and select the
.sqlfile you exported from your local database. - 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
publicsubdirectory 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_htmlto/home/youruser/public_html/your_project_folder/public. - Method 2: Using a
.htaccessRedirect (More common for shared hosting without Document Root changes) If you cannot change the Document Root, you can use an.htaccessfile in your mainpublic_htmldirectory (or the root of your domain/subdomain) to redirect all requests to thepublicsubdirectory.- In cPanel File Manager, navigate to your main
public_htmldirectory (or the root directory for your domain). - Click “+ File” to create a new file. Name it
.htaccess(ensure you include the dot at the beginning). - Edit the
.htaccessfile and add the following code:
- In cPanel File Manager, navigate to your main
<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 insideyour_project_folder/public/. Replaceyour_project_folderwith 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_folderis the correct name of the directory containing your Laravel project files withinpublic_html.
- Explanation:
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
vendorfolder 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
cdinto your project directory and runcomposer install.
- Option A (Less Ideal but sometimes necessary): Upload the
- 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 yourAPP_KEYin the.envfile, 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 theAPP_KEYin your.envfile. You can generate one locally withphp artisan key:generateand 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 frompublic/storagetostorage/app/public. This is needed to display files uploaded viaStorage::disk('public'). If you can’t run commands, you might need to manually copy files fromstorage/app/publicto 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
storageandbootstrap/cachedirectories need to be writable by the web server user. - Using cPanel File Manager:
- Right-click on the
storagefolder within your project root. - Select “Change Permissions”.
- Set the permissions to
755(or775or777if755doesn’t work, but777is less secure). Ensure “Apply changes to files and subfolders” is checked. - Repeat the process for the
bootstrap/cachefolder.
- Right-click on the
- 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
755or775for directories and644or664for 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:clearor 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.phpfile 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-cacheonce, then immediately remove this route fromweb.phpand 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
- Find Databases: In the hPanel sidebar, look for the “Databases” section and click on “Management”.
- 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:
- In hPanel, find the “Files” section and click on “File Manager”. This will open a new tab with the web-based file manager.
- Navigate to the directory for your domain. This is usually
domains/yourdomain.com. - Important: As with cPanel, upload the contents of your Laravel project directory excluding
vendorandnode_modules. Compress your project locally into a.zipfile without thevendorfolder. - Click the “Upload” icon (usually a cloud with an arrow) at the top. Select “File” and choose your
.zipfile. - Once uploaded, right-click the
.zipfile and select “Extract”. Enter the destination directory (e.g.,domains/yourdomain.com). - After extraction, delete the
.zipfile.
- Using an FTP/SFTP Client:
- Get your FTP/SFTP credentials from hPanel (usually under “Files” > “FTP Accounts”).
- Connect using your FTP/SFTP client.
- Navigate to your domain’s directory on the server (e.g.,
domains/yourdomain.com). - Upload all files and folders from your local Laravel project except the
vendordirectory.
Step 4: Configure the .env File
- Locate the
.envfile: 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. - Rename and Edit: Rename
.env.exampleto.env. - Edit the
.envfile: Edit the.envfile and update theDB_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- (Use
httpsif you have an SSL certificate installed). - Set
APP_DEBUG: SetAPP_DEBUG=false. - Save Changes: Save the
.envfile.
Step 5: Import Your Database
- Find phpMyAdmin: In hPanel, go back to “Databases” > “Management” and click the “phpMyAdmin” button next to your database.
- Select Your Database: In the left sidebar of phpMyAdmin, click on your database name.
- Click the “Import” Tab: Click the “Import” tab at the top.
- Choose File: Click “Choose File” and select your exported
.sqlfile. - 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.
- Find “Set webiste root”: In hPanel, navigate to the “Website” section and click on “Set website root”.
- Select the
publicfolder: Click the “Select folder” button. Navigate through your file structure (domains/yourdomain.com/your_project_folder) and select thepublicfolder. - Confirm: Click the “Select this folder” button. This tells Hostinger’s web server to serve files from within that
publicdirectory.
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.
- Enable SSH: Go to “Advanced” > “SSH Access” and enable it. You’ll get connection details (hostname, port, username, password).
- 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.
- Navigate to Project: Once connected, navigate to your Laravel project root directory (e.g.,
cd domains/yourdomain.com/your_project_folder). - 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
vendorfolder (less ideal), manually generate theAPP_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
storageandbootstrap/cachefolders, select “Permissions”, and set them to755(or775). 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=trueTemporarily: In your.envfile, setAPP_DEBUG=true. This will display the actual Laravel error message in your browser. REMEMBER TO SET IT BACK TOfalseAFTER DEBUGGING!
- Permissions Issues: If you see errors related to files or directories not being writable, double-check the permissions of the
storageandbootstrap/cachedirectories (should be755or775). - Database Connection Errors: Verify the database credentials (
DB_DATABASE,DB_USERNAME,DB_PASSWORD,DB_HOST) in your.envfile exactly match the database and user you created on the server. APP_KEYNot Set: If you see errors about encryption keys, ensureAPP_KEYis set in your.envfile. It should be a 32-character random string. Generate one locally withphp artisan key:generateif needed.- Files Not Found (404 Errors):
- Ensure your web server is pointing to the
publicdirectory (Step 6 in both sections). - Check that you uploaded all necessary files and folders (excluding
vendor). - Verify your
.htaccessfile (if using that method) is correctly placed and configured.
- Ensure your web server is pointing to the
- 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 fromstorage/app/publictopublic/storage.
- Composer Dependencies Missing: If you uploaded without the
vendorfolder and couldn’t runcomposer install, you’ll get errors about classes not found. You’ll need to find a way to runcomposer installon the server or upload thevendorfolder (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/