Deploying a Laravel application from local development to production involves several steps. Below is a comprehensive guide to help you through the process, especially if you plan to use Laravel Forge and Digital Ocean.
Step 1: Prepare Your Local Environment
-
Ensure Your Code is Ready for Production:
- Make sure your code is clean and tested.
- Remove any debug code or unnecessary files.
-
Update Your
.envFile:- Ensure your
.envfile is configured correctly for production. For example:APP_ENV=production APP_DEBUG=false APP_URL=https://yourdomain.com
- Ensure your
Step 2: Push Your Code to a Version Control System
-
Initialize a Git Repository:
- If you haven't already, initialize a Git repository in your project directory:
git init
- If you haven't already, initialize a Git repository in your project directory:
-
Commit Your Changes:
- Add and commit your changes:
git add . git commit -m "Initial commit"
- Add and commit your changes:
-
Push to a Remote Repository:
- Push your code to a remote repository like GitHub, GitLab, or Bitbucket:
git remote add origin https://github.com/yourusername/yourrepository.git git push -u origin master
- Push your code to a remote repository like GitHub, GitLab, or Bitbucket:
Step 3: Set Up Your Server with Laravel Forge
-
Create a New Server:
- Log in to Laravel Forge and create a new server. Choose Digital Ocean as your provider and configure the server settings.
-
Deploy Your Site:
- Once the server is ready, create a new site on the server. Forge will ask for your Git repository URL and branch to deploy.
-
Configure Environment Variables:
- In Forge, go to the "Environment" tab of your site and set the necessary environment variables. This will replace the
.envfile in your local environment. For example:APP_ENV=production APP_DEBUG=false APP_URL=https://yourdomain.com DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=yourdatabase DB_USERNAME=yourusername DB_PASSWORD=yourpassword
- In Forge, go to the "Environment" tab of your site and set the necessary environment variables. This will replace the
Step 4: Deploy Your Application
-
Deploy the Code:
- In Forge, click the "Deploy" button to deploy your code to the server.
-
Run Migrations and Seeders:
- After deployment, you may need to run database migrations and seeders:
php artisan migrate --force php artisan db:seed --force
- After deployment, you may need to run database migrations and seeders:
-
Set Up Storage and Cache:
- Ensure the storage and cache directories are writable:
php artisan storage:link php artisan config:cache php artisan route:cache php artisan view:cache
- Ensure the storage and cache directories are writable:
Step 5: Configure Your Domain
-
Point Your Domain to the Server:
- Update your DNS settings to point your domain to the IP address of your Digital Ocean server.
-
Set Up SSL:
- In Forge, you can easily set up an SSL certificate using Let's Encrypt. Go to the "SSL" tab of your site and follow the instructions.
Step 6: Monitor and Maintain
-
Monitor Your Application:
- Use tools like Laravel Telescope, Sentry, or New Relic to monitor your application for errors and performance issues.
-
Regular Backups:
- Set up regular backups for your database and important files.
By following these steps, you should be able to successfully deploy your Laravel application from local development to a production environment using Laravel Forge and Digital Ocean.