Laravel on Sub-Domain of shared server

Last updated: Friday 19th April, 2024 06:08 PM IST
  1. Home
  2. Articles
  3. Laravel on Sub-Domain of shared server
Laravel on Sub-Domain of shared server

Laravel on Sub-domain

Let us learn how to host a laravel project on a sub-domain of shared hosting.

Assumptions / Pre-requisites

I presume that you have the following things ready with you.

  • Shared hosting server
  • Sub-domain on that shared hosting server
  • SSH Access
  • Git on that shared hosting server

Installation steps

Follow along to host the laravel application on a subdomain of a shared hosting server.

SSH to your server

First, we will SSH into our shared hosting provider. Run the following command in your terminal.

ssh -p <port_number> <username>@<host-ip-address>

Replace <port_number> with your host's port_number. Replace <username> with your host account's username. Replace <host-ip-address> with your host account's IP Address.

Change the directory to your domain's directory

Now, we will change the directory to where all the domains reside. Generally, this is in the home directory (~). But if your all domains directory is at some other location, navigate to that directory.

cd ~/domains

Clone the repo

Inside the domains directory, clone the repository from GitHub, GitLab, or anywhere else.

git clone <repo-url> <sub-domain-name>

Replace the <repo-url> with your actual repository URL. Replace the <sub-domain-name> with your sub-domain name.

Change the directory

Now, we need to change the directory to the real domain name.

cd ~/domains/<main-domain>

Replace <main-domain> with your real domain name.

Delete the subdomain directory

Now, we need to delete the sub-domain directory that was created inside the main-domain directory.

rm -rf <sub-domain-name>

Replace the <sub-domain-name> with your sub-domain name.

Navigate to sub-domain

We will, now, change the directory to the cloned sub-domain directory.

cd ~/domains/<sub-domain-name>

Replace <sub-domain-name> with the name of your sub-domain.

Create a symlink

We will now create a symlink between our cloned Laravel app's public directory and the sub-domain directory.

ln -s ~/domains/<sub-domain-name>/public/ ~/domains/<main-domain-name>/public/<sub-domain-name>

Replace <main-domain-name> with your main-domain name. Replace sub-domain-name with your sub-domain name. You can now check in the browser by navigating to the main domain's directory. You will have a new directory created called <sub-domain-name>. This directory points to the public/ directory of the cloned repository.

Install dependencies

Now that we have created the symlink, we need to install all the package dependencies used by our Laravel application. So execute the following command inside your sub-domains directory.

composer install --optimize-autoloader --no-dev

Please be careful regarding the composer when installing. Sometimes, the hosting providers want us to replace composer with composer2. If you don't have the composer installed (which by default is installed, just a different version), install it manually by going through the composer's documentation.

Add and Update environment variables

By default, laravel ships the .env.example file. It is highly recommended that you add the keys only in this file, not their values unless it is okay to add values.

cp .env.example .env

Once copied, add the necessary values in the newly created .env file. Details like mail config, database config, etc. Also, make sure the APP_ENV is set to production, and APP_DEBUG is set to false. We don't want people to know that our app is exposed.

Application key

For our application to work, we need the application key. Execute the following command:

php artisan key:generate

Database migration

Next comes the migration of the database. If your application does not require the database, feel free to skip this step

php artisan migrate --seed

After migrating, we will also seed the database with the default data we might have added in the seeder file. Feel free to discard the --seed if it is not required.

Storage link

Sometimes, our application requires uploaded files to be saved on our servers. Thus, we need to symlink the storage path with the public path. Execute the following command

php artisan storage:link

This will create a symbolic link between the storage/app/public and public/storage directory.

Build directory

If your application uses InertiaJs with Vue or ReactJs you need to have the Vite's manifest file in the public/build directory. This build directory gets created after you run the following command

npm run build

Once it is done, you will have to manually upload the build directory in the public folder of the application. But if you have node and nvm installed on your shared server, you can do it easily by running the above npm command.


Yay! You have successfully deployed your Laravel application on a sub-domain of the shared hosting server.

Visit https://subdomin.maindomain.com to view your laravel app.


Courtesy Links

Mehul Bawadia

Author:

Mehul Bawadia is a Full Stack Developer in Laravel and VueJs, based in Mumbai, India. He has an overall experience of 8 years in the website development field. When he is not working, you will find him learning new stuff that is not related to work.

Process Followed

1. Discover

In this process, I learn more about the requirements from you and/or from the client, and come up with varios permutations and combinations to meet the requirements.

2. Design

Once I learn properly, I do the design of the requirements that you gave keeping things aesthetically pleasing & useable for your audience.

3. Develop

Once you and/or the client is happy with the design(s), I start with the development process of the said requirements.

4. Deploy

After development, I will send the developed task to the client for reviewing. Once confirmed, will be deployed to the live server.

Tech Stack

HTML 5

HTML 5

CSS 3

CSS 3

TailwindCSS

TailwindCSS

JavaScript

JavaScript

PHP

PHP

MySQL

MySQL

Laravel

Laravel

VueJs

VueJs

Spare time Projects

Few of the simple projects simply to learn.

View Projects
Copyright © 2024, BMehul. All Rights Reserved.
Built with A heart icon by Mehul Bawadia.
An arrow that takes you to the top of the page when clicked