Laravel Shared Hosting Setup: A Complete Guide to Deploy Your App on Cpanel
Shared hosting might not be an ideal server for laravel applications, but considering cost-friendly option, you might need to learn about hosting your laravel app in these hosting too. Specially, when you need to find a cheap option for saving budget, you can go for shared hosting. Though its not a very straight forward option, we can do it in several steps. Lets teach ourselves with the process in this article.
Deploying Your Laravel App on Shared Hosting: A Step-by-Step Guide
Assume you have a working laravel application in your local machine. Also you will need access to a shared hosting cpanel. We will need these things along with the way.
- Lets start with setting up the database
First we need to upload the database to cpanel. Please log in to Cpanel and find MYSQL Databases in the dashboard.
Give a new name for the database and click Create Database
Then create a new user from this section. You can use Password Generator to create a strong password, but please save the password for future reference.
Now add newly created user to the database, it will take you to a screen, where you need to give permission to this user. Allow all the permission for the user.
Now please go to Cpanel Dashboard again, look for phpMyAdmin. Click on it, you will see your newly created database is visible there.
- Deploy local database
Now we need to upload our local database to Cpanel’s database. For that you need to export your local database. Select the database and click on export button, it will create a mysql file in the local.
Then go to cpanel’s database, select it and click on import. Choose the mysql file and click import
If these steps are done correctly, we have successfully imported mysql file to Cpanel’s Database.
- Lets upload our application
Now please go to your laravel application’s local directory. Compress all the files as .zip file.
Then go to Cpanel’s public_html folder, clear the folder if there are any existing files.
Click on upload in the directory, and select the zipped file for uploading. It will take a bit time to upload the file depending on your application size.
Please extract the files after uploading is complete.
- Setup the env
Now in the server’s env file, we need to connect our database to our application. To do that, please provide the database name, user and password, as we saved before while setting up the database and user
DB_DATABASE=
DB_USERNAME=
DB_PASSWORD=
You can also change other values as required for the live server, for example APP_URL or mail server credentials.
Shared Hosting with Laravel: Guide to Accessing Your App at Root URL
After uploading a laravel application to shared hosting, you cannot directly access to application via root domain. For example, if your domain is https://example.com, you can access site to https://example.com/public URL. To resolve this issue we can take different steps, here is the guide below.
- Method 1: Copy all the contents of public folder to root directory
In this way, select all contents under public directory and move it to root directory (public_html).
Then open index.php file, which is moved from public folder.
And edit these lines below:
if (file_exists($maintenance = __DIR__.'/../storage/framework/maintenance.php')) {
require $maintenance;
}
To
if (file_exists($maintenance = __DIR__.'/storage/framework/maintenance.php')) {
require $maintenance;
}
and:
require __DIR__.'/../vendor/autoload.php';
To
require __DIR__.'/vendor/autoload.php';
And:
$app = require_once __DIR__.'/../bootstrap/app.php';
To
$app = require_once __DIR__.'/bootstrap/app.php';
just remove the ../ from each case.
Now we are done here. You should be now able to access your application via root domain.
- Method 2: Using a htaccess file
In this method, we dont need to move all our files from public folder to root directory. Rather, we will use a .htaccess file to redirect the route to public folder.
Create a .htaccess file in public_html folder.(Assuming you have uploaded your application to public_html). Now edit the file like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
Lets try to understand what this code does. This code will redirect all the traffic or requests from root directory to public folder, which is required to load our application in root domain.
Among these steps, I think the second step is much easier to do. Because every time you would deploy your changes to server, each time you would have to copy all the contents to root folder and modify index.php with method 1, which is bit complex. So setting up .htaccess file will help you to get rid of extra steps.
Fine-Tuning Your PHP Server: How to Tweak Directives for Optimal
For working in any php server, you need to know about php directives or php ini settings. These are the values which controls how you can perform different operations up to a certain limit. For example, these operations might include tasks like accommodating larger files, longer execution times, and more complex applications. In php servers like XAMPP, MAMP, WAMP or even in Cpanel, there are certain default values always provided for these variables but that might not suit your necessities. You need to tweak those values as required to your application or requirements. In this article, lets try to understand and learn about how to manage these variables for our own convenience.
- What are most common directives?
There are many directives that can affect your application and performance for different actions. Lets discuss most common variables you will need to work with:
1. Large File Uploads:
Upload_max_filesize: Controls the maximum size of an uploaded file. If users need to upload large files (e.g., videos, high-resolution images, large datasets), you will need to increase this
Post_max_size: This setting demonstrate what should be the size of POST data. It's often set higher than upload_max_filesize to account for additional POST data.
2. Handling Many File Uploads Simultaneously:
max_file_uploads: Sets the maximum number of files that can be uploaded simultaneously. If your application allows uploading multiple files at once, you may need to increase this limit.
3. Complex and Long-Running Scripts:
max_execution_time: The maximum time in seconds a script is allowed to run before it is terminated by the parser. For scripts that perform complex calculations, interact with external APIs, or process large amounts of data, you might need to increase this limit to prevent timeouts.
max_input_time: The maximum time in seconds a script is allowed to parse input data (e.g., POST and GET data). Increase this limit if your script needs more time to process large input datasets.
4. Handling Large Forms or Multiple Variables:
max_input_vars: Limits the number of input variables your script can handle (e.g., form fields). If your forms or requests contain many variables, you need to increase this limit to ensure all data is processed.
5. High Memory Usage:
memory_limit: Sets the maximum amount of memory a script is allowed to allocate. Scripts that process large amounts of data, perform complex operations, or handle many simultaneous users may require more memory.
- How would I check my server’s settings
To check your local php ini settings, first create a php file (info.php) under your webserver’s root directory (usually htdocs or www).
Then edit the file like below:
<?php phpinfo(); ?>
After that, open this file in your web browser by navigating to http://localhost/info.php.
This will show your server’s current settings and limits.
- How would I change my server's limits?
To change php.ini limits for your server, you need find php.ini file in the server first. Here are the locations for different server you can look for the file:
- For XAMPP: C:\xampp\php\php.ini
- For WAMP: C:\wamp\bin\php\php[version]\php.ini
- For MAMP (Mac): /Applications/MAMP/bin/php/php[version]/conf/php.ini
For cpanel, you can modify these values by navigating to "Select PHP Version" or "PHP Selector"
Now open the php.ini file, and update these values as given below:
upload_max_filesize=2000M
max_file_uploads=20000
post_max_size=8000M
max_execution_time=30000
max_input_time=600000
max_input_vars = 1000000
memory_limit=1000M
Note: It can be difficult for you to find these values in the file, because php.ini is a large file with many configuration settings. So, you can use ctrl+F to find the actual variable. Also, you might see some variables are commented out like this ;upload_max_filesize. In such case, just remove the ; from beginning to make those active and effective.
These values provided here are maximum limits, which will help you not to get error potentially when you work in your application. But increasing these limits also makes your application bit vulnerable in terms of security. So you can give a lower limit also if that works for you.
For cpanel, you might not have to edit any php.ini file, rather under "Select PHP Version" or "PHP Selector", you can find exactly these fields with default values.. just make the values updated as per your requirement and save it.
Summary
We have learnt about how to post Laravel application to shared hosting. But you can also follow the procedure for any PHP application. Also, as we are using shared hosting, you need to redirect public assets to root URL. PHP directives are not something that you need to work for every day. You just have to configure these once according to your own requirement. But working with only default values will most probably cause issues while working or testing your application in the server. Because, whenever these limits are exceeded, which can be triggered from a request of your application, you will see errors which might bother you. So, please do some checks and trials to understand which values might help you for running your most of the applications smoothly.
So thats all from this article today. Thank you for reading this post. I hope you found this helpful. Please share your thoughts in the comments below.
I wish you a happy day.
Comments