How To Deploy a Django Application on Shared Hosting

Here you will lean how to deploy your Django application on shared hosting using SSH.
10th Mar 2024
Table Of Content
What is Shared Hosting? Why is it hard to deploy Laravel on Shared Hosting?PrerequisitesInstalling Essential ToolsDeploying Your Laravel ProjectConclusion
Albin Varghese
Wexron Team
Deploying a Laravel Application on Shared Hosting: A Comprehensive Guide
As a developer, deploying a Laravel application on shared hosting can be a daunting task, especially if you're new to the process. However, with the right guidance, you can successfully deploy your Laravel project on shared hosting. In this article, we'll take you through a step-by-step guide on how to deploy a Laravel application on shared hosting, covering the prerequisites, installation of essential tools, and configuration of your Laravel project.
What is Shared Hosting? Why is it hard to deploy Laravel on Shared Hosting?
Shared hosting is a type of web hosting where multiple websites share the same server and resources. This type of hosting is ideal for small to medium-sized websites and projects, as it is cost-effective and easy to manage. However, deploying a Laravel application on shared hosting can be challenging due to the limitations imposed by the hosting provider, such as restricted access to the server, limited resources, and security restrictions.
Prerequisites
Before we begin, make sure you have the following:
- An active shared hosting plan with a reputable provider
- Basic knowledge of Git (optional, but recommended)
- SSH access (optional, you can refer FTP/SFTP articles if you don't have SSH access)
- A Laravel project ready to be deployed (for this guide, we will use a sample project for the sake of demonstration)
Installing Essential Tools
Before we deploy our Laravel project, we need to install some essential tools to ensure a smooth deployment process.
Installing Git
Git is a version control system that allows you to track changes to your code and collaborate with others. If you have SSH access, Git would probably be available on your account, so you won't need to install it separately. If you're new to Git, don't worry – we'll cover the basics as we go along.
To verify your Git installation, run the following command in your terminal:
git --version
If Git is not installed, you can contact your hosting provider's support team for assistance. By default, most shared hosting providers offer Git as part of their service.
Installing Composer
Composer is a dependency manager for PHP that allows you to easily install and manage dependencies for your Laravel project. Most shared hosting providers offer a globally installed Composer. You can verify your Composer installation by running the following command in your terminal:
composer --version
If Composer is not installed, you can follow these steps to install it manually (applicable to directadmin & cPanel):
- Connect to your server via SSH
- Navigate to the root directory (
/home/{username}
) - Create a
bin
folder if it doesn't exist - Install the Composer setup script:
cd ~
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php composer-setup.php --filename=composer --install-dir=bin
php -r "unlink('composer-setup.php');"
chmod +x bin/composer
Verify your Composer installation with: composer --version
, it should display the installed version.
Composer version 2.6.3 2023-09-15 09:38:21
Installing npm/yarn
npm (Node Package Manager) and yarn are package managers for JavaScript that allow you to easily install and manage dependencies for your Laravel project. Installing npm/yarn is essential if your project uses JavaScript packages or frontend frameworks like Vue.js or React. Most shared hosting does not come with npm/yarn pre-installed, so you'll need to install them manually. With SSH access, you can easily setup your preferred Node.js version for your project. We recommend using NVM (Node Version Manager) to install npm/yarn:
# Install NVM using curl:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash
# or using wget:
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash
Once NVM is installed, you can easily set up your preferred Node.js version for your project. NVM will handle the rest.
# Install the latest LTS version of Node.js
nvm install --lts
# check the installed Node.js version
node -v
npm -v
That's it! You've all the essential tools installed and ready to deploy your Laravel project on shared hosting.
Deploying Your Laravel Project
Now that we have our essential tools installed, it's time to deploy our Laravel project. We'll cover the deployment process using SSH, but you can also refer this guide to deploy your project using FTP/SFTP.
Step 1: Clone Your Laravel Project
To deploy your Laravel project, you'll need to clone your project's repository to your shared hosting account. Using Git, you can clone your project using the following command: git clone {repository_url}
.
Make sure to upload your project to the correct directory, which is usually public_html
or domains/{yourdomain.com}/public_html
. For the sake of demonstration, we'll use a sample Laravel project hosted on GitHub
# Navigate to your project directory
cd ~/domains/{yourdomain.com}
# remove the existing public_html folder
rm -rf public_html
# clone the sample project
git clone https://github.com/albinvar/simple-social-media.git public_html
# navigate to the project directory
cd public_html
Step 2: Install Composer Dependencies
Once you've cloned your project, we need to install the Composer dependencies. You can do this by running the following command in your project directory:
composer install
# or if you want to skip the dev dependencies
composer install --no-dev
Step 3: Configure Your Laravel Project
After installing the Composer dependencies, you'll need to configure your Laravel project. This involves setting up your .env
file, generating a Laravel application key, and running the necessary migrations.
Laravel applications use a .env
file to store configurations. To create the main .env
file, use the example .env
file provided:
# copy the .env.example file
cp .env.example .env
After creating your .env
file, you can edit the configurations for the database and other services. For configuring emails and databases, refer to the relevant articles.
Once your environment variables are set, generate your Laravel application key:
# generate the Laravel application key
php artisan key:generate
Step 4: Configuring Your Domain Root
For a typical Laravel application, you'll need to configure your domain root to point to your project's public folder. One way to achieve this is by creating an .htaccess
file in your domain root and using it to route all requests to public/index.php
.
# create the .htaccess file
touch .htaccess
# edit the .htaccess file
nano .htaccess
Add the following content to your .htaccess
file:
<IfModule mod_rewrite.c>
# That was ONLY to protect you from 500 errors
# if your server did not have mod_rewrite enabled
RewriteEngine On
# RewriteBase /
# NOT needed unless you're using mod_alias to redirect
RewriteCond %{REQUEST_URI} !/public
RewriteRule ^(.*)$ public/$1 [L]
# Direct all requests to /public folder
</IfModule>
If you're using a subdomain, you can edit the path to your Laravel project's public folder directly from your hosting provider's control panel without the need for an .htaccess
file.
Step 5: Running Migrations and Seeding
If your project requires a database, follow these steps to create a MySQL database:
- Login to your hosting provider's control panel
- Navigate to "MySql Management"
- Create a new Database with the necessary details
- Note down or copy the configuration values
- Open
.env
and configure your connection with your Laravel application
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=your_database_name
DB_USERNAME=your_database_username
DB_PASSWORD=your_database_password
- Run the migrations to create the necessary tables in your database, if you have any seeders, you can run them as well:
# run the migrations
php artisan migrate
# run the seeders
php artisan db:seed
Step 6: Installing npm/yarn Dependencies (if applicable)
Most laravel projects come with frontend dependencies that need to be installed using npm/yarn. If your project uses npm/yarn, you can install the dependencies by running the following command in your project directory:
# install npm dependencies
npm install
# or if you're using yarn
yarn install
Step 7: Building Your Assets (if applicable)
If your project uses frontend frameworks like Vue.js or React, you'll need to build your assets before deploying your project. You can do this by running the following command in your project directory:
# build your assets
npm run build
# or if you're using yarn
yarn build
Step 8: Testing Your Laravel Application
Once you've completed the deployment process, you can test your Laravel application by visiting your domain in a web browser. If everything is set up correctly, you should see your Laravel application up and running.
Congratulations! 🎉 You've successfully deployed your Laravel project on shared hosting.
if you face any issues during the deployment process, you can always reach out to your hosting provider's support team for assistance. At wexron hosting, we offer 24/7 support to help you with any issues you may encounter during the deployment process.
For a live demonstration of a successfully deployed Laravel project, visit https://social.w3x.live.
Conclusion
Deploying a Laravel application on shared hosting is a straightforward process that requires some basic knowledge of Git, Composer, and Laravel. By following this guide, you should be able to successfully deploy your Laravel project on shared hosting. Remember to take advantage of your hosting provider's support team and resources to ensure a smooth deployment process.
Table Of Content
What is Shared Hosting? Why is it hard to deploy Laravel on Shared Hosting?PrerequisitesInstalling Essential ToolsDeploying Your Laravel ProjectConclusion
Albin Varghese
Wexron Team