You are currently viewing Easily deploy Laravel 10 Application to a Shared Host with Vite

Easily deploy Laravel 10 Application to a Shared Host with Vite

  • Reading time:8 mins read

How to Deploy a Laravel 10 Application To A Shared Host With Vite

Laravel is a popular PHP framework that provides a simple and elegant way to build web applications. Vite is a next-generation frontend tool that offers fast development, hot module replacement, and production-ready optimization. In this article, we will learn how to deploy a Laravel 10 application to a shared host with Vite.

Prerequisites

Before we begin, we need to have the following:

  • A Laravel 10 application that uses Vite for the frontend. You can follow this tutorial to create one here.
  • A shared hosting account that supports PHP 8 and Composer. We will use Hostinger as an example, but you can use any other provider that meets these requirements.
  • An FTP client such as FileZilla or Cyberduck to upload files to the server.
  • A domain name that points to the shared host.

Step 1: Build the Laravel 10 Application

The first step is to build the application for production. This will generate optimized files for the frontend and backend that are ready to be deployed. To do this, open a terminal and navigate to the root directory of your Laravel project. Then run the following commands:

composer install --no-dev
npm run build
php artisan optimize

Copy

The composer install --no-dev command will install the PHP dependencies without the development packages. The npm run build command will invoke Vite to bundle and minify the frontend assets. The php artisan optimize command will cache the configuration and routes for faster performance.

Step 2: Upload the Files

The next step is to upload the files to the shared host. To do this, open your FTP client and connect to your server using the credentials provided by your hosting provider. Then navigate to the public_html folder, which is where your website files should be stored. If you have other websites on the same account, you may need to create a subfolder for your Laravel application.

Once you are in the public_html folder, upload all the files and folders from your Laravel project, except for the node_modules folder, which is not needed on the server. You can also delete the .env.example file, which is only used for local development.

Step 3: Configure the Environment Variables

The next step is to configure the environment variables for your Laravel application. These are stored in the .env file, which is located in the root directory of your project. You need to edit this file and update some values according to your server settings.

The most important variables are:

  • APP_URL: This should be set to your domain name, such as https://example.com.
  • DB_CONNECTION, DB_HOST, DB_PORT, DB_DATABASE, DB_USERNAME, DB_PASSWORD: These should be set to your database credentials, which you can obtain from your hosting provider.
  • APP_KEY: This should be set to a random 32-character string, which you can generate using this tool: https://randomkeygen.com/. This key is used for encryption and hashing purposes.

You can also modify other variables according to your preferences, such as APP_NAME, APP_ENV, APP_DEBUG, etc.

Step 4: Migrate the Database

The next step is to migrate the database tables for your Laravel application. To do this, you need to run the php artisan migrate command on the server. However, since most shared hosts do not allow SSH access, you need to use an alternative method.

One option is to use Laravel’s web-based artisan console, which you can enable by adding this route to your routes/web.php file:

Route::get('/artisan/{command}', function ($command) {
    \Artisan::call($command);
    return \Artisan::output();
});

Copy

This route will allow you to execute any artisan command by visiting a URL like https://example.com/artisan/migrate. However, this is very insecure and should only be used temporarily for deployment purposes. You should remove or comment out this route after migrating the database.

Another option is to use a third-party service such as Laravel Forge or Envoyer, which can automate the deployment process and run artisan commands for you. However, these services require a subscription fee and may not support all shared hosts.

Step 5: Test the Application

The final step is to test the application and make sure everything works as expected. You can visit your domain name and browse through your website pages. You can also check if there are any errors in the storage/logs/laravel.log file or in your browser console.

If you encounter any issues, you may need to troubleshoot them by following these steps:

  • Clear the cache by running php artisan cache:clear and php artisan config:clear on the server or using the web-based artisan console.
  • Check the file permissions and make sure the storage and bootstrap/cache folders are writable by the web server user.
  • Check the .htaccess file and make sure it has the correct rules for Laravel. You can use the default .htaccess file that comes with Laravel or follow this guide.
  • Check the PHP version and extensions and make sure they match the Laravel requirements. You can use the phpinfo() function to get this information or contact your hosting provider.

Conclusion

In this article, we learned how to deploy a Laravel 10 application to a shared host with Vite. We covered the steps of building, uploading, configuring, migrating, and testing the application. We also discussed some possible issues and solutions.

Deploying a Laravel application to a shared host can be challenging, but it is not impossible. With some patience and troubleshooting skills, you can successfully launch your website and enjoy the benefits of Laravel and Vite.🚀