It's time to deploy this to a live server! But for this, we need a few things:
- Point a domain name to the server's IP address.
- Run a checklist to see that everything is up and ready.
- Do a silent launch to a few users to see if everything works.
So let's get started!
Pointing a Domain Name
Pointing a domain to our server requires you to access your DNS management tool. It can be one of:
- Go Daddy
- Namecheap
- Cloudflare
- etc.
In that tool, you have to create a DNS record that would point to your forge server IP address:
Once this is pointed out, we have to change the Site Domain
in Forge:
Visit your Forge, enter the server, and click on the site you want to deploy. Then click on the Settings
sidebar item:
Here, you have to change the Site Domain
to the domain you pointed to the server:
This will make our develop
server accessible via the domain name.
Now for production, there are two ways to do this:
- Create a new Server and point the domain to it. This is the best way to do it, as it will keep the
develop
server for development purposes only. - Add another Site to the same server and point the domain to it. This might be cheaper but will mix the production and development environments.
We will add another site to the same server for this tutorial's sake. So let's do that:
Note: To create a new production server, follow the same steps as develop
server creation.
To add a site to any server, visit the server, click on the Sites
sidebar item, and click on the New Site
:
Enter your domain name and click on the Add Site
button.
Note: You must point the domain to the new IP address for a separate server.
Once the site is added, we can add a Database to it. Click on the Databases
sidebar item and fill out the Add Database
form:
Next is our Git Repository
installation. Go back to your site and click on the Git Repository
sidebar item. Fill out the form with your repository details:
Note: Select main
as your branch, and remember to select the newly created database.
This should open a familiar Application
page (the same as our dev
server). From here, we have to go through a checklist to see if everything is up and running.
Running a Checklist
Checklists are a great way to configure a server. We recommend you create it for each project and adapt it to your needs. Here is a basic checklist that you can use:
- The APP key is set
- The APP URL is set
- Database settings are correct
- Storage link is created
- Google Secrets are set
- OpenAI Secrets are set
- AWS S3 secrets are set
- Deployment Script is set
- Sentry is set
- Queue is set
- SSL is enabled
Of course, there are more things. For example, if your project uses a caching
system, you should add that to the checklist.
Now, let's look at our servers:
For dev
, we need to change our APP_URL
since we just added a domain name and added an SSL
certificate:
First, visit the Environment
sidebar item and change the APP_URL
to the domain name:
We have changed this to our domain http://dev.linksletter.com
But now, if we try to load the page, we will see an SSL error:
So, let's add an SSL certificate. Visit the SSL
sidebar item and click on the Let's Encrypt
button:
Leave everything as is and click on the Obtain Certificate
button. Please wait for it to finish, and then try to load the page again. It should load without any errors:
And for production, we have to set everything up. So let's do that:
Configuring .env File
Let's open the Environment
sidebar item and fill in:
-
APP_URL
-
SENTRY_LARAVEL_DSN
-
GOOGLE_CLIENT_ID
-
GOOGLE_CLIENT_SECRET
-
OPENAI_KEY
-
AWS_ACCESS_KEY_ID
-
AWS_SECRET_ACCESS_KEY
-
AWS_BUCKET
Once that is done, save the changes.
Note: Depending on your project, You might need to add or change more variables.
Adding Deployment Script
To have a deployment script, we can use the dev
server script:
# The first line should be `cd ...` to navigate to the project directory - keep it as is! if [ -f artisan ]; then $FORGE_PHP artisan downfi git pull origin $FORGE_SITE_BRANCH$FORGE_COMPOSER install --no-dev --no-interaction --prefer-dist --optimize-autoloader npm installnpm run build rm -rf /node_modules if [ -f artisan ]; then $FORGE_PHP artisan cache:clear $FORGE_PHP artisan view:clear $FORGE_PHP artisan route:cache $FORGE_PHP artisan config:cache $FORGE_PHP artisan view:cachefi ( flock -w 10 9 || exit 1 echo 'Restarting FPM...'; sudo -S service $FORGE_PHP_FPM reload ) 9>/tmp/fpmlock if [ -f artisan ]; then $FORGE_PHP artisan migrate --force $FORGE_PHP artisan upfi
In fact, the only difference is the first line—navigating to the project. Other than that, your dev
and production
scripts should stay identical. This helps you see production issues before hitting the production server.
Adding Queue
Let's add a Queue worker by visiting Queue
in the sidebar:
In the form, we will change:
- Connection to
database
- since we use the database for the queue. (you can useredis
, but then you have to change.env
filesQUEUE_CONNECTION
toredis
) - Check the
Always Run, Even In Maintenance Mode
- this will keep the queue running even if the site is in maintenance mode.
And that's it! Our base queue is ready.
Note: You can read more about Queues in Production on our Practical Laravel Queues on Live Server
Adding SSL
We have to repeat what we did for the dev
server for SSL. Visit the SSL
sidebar item and click the Let's Encrypt
button. Wait for it to install, and you should have SSL enabled.
Linking Storage
This one is pretty simple, just SSH into the server and run the following command:
cd /home/forge/v2.linksletter.comphp artisan storage:link
First Deployment
Now that everything is set up let's make the first deployment. Visit the Deployment
sidebar item and click the Deploy Now
button. This should deploy the site to the server.
That's it! Now, we have our checklist done. Next is to do a silent launch.
Silent Launch
Now, here's a real problem - we are live on both servers, but we are not ready to give the link to the public. How about we do a silent launch?
For this, we can use the maintenance mode
! Laravel allows us to set a key
to gain access while it's in maintenance mode. So let's do that:
Visit your Site in Forge and look for this button:
Click on it and generate the secret
:
Then hit Enable Maintenance Mode
, and your site will be in maintenance mode. But if you visit the site, you will see a maintenance page. To bypass this, visit your site with the secret
key:
https://dev.linksletter.com/your-secret
just like this https://dev.linksletter.com/lnop3xpfh95k6fkz707h
This should load the site as if it were not in maintenance mode. You can now share this link with your team to test the site.
Of course, this was just for the dev
server. We need to do the same for the production server.
After this is done, we need to slightly modify our Deployment script. For both dev
and production
comment the following lines out (by adding #
in front)
#$FORGE_PHP artisan down // ... #$FORGE_PHP artisan up
If we don't do this, our deployment will remove the maintenance mode, and the site will be live. We want something else. We want to keep the site in maintenance mode until we are ready to launch it.
That's it for the basics of deploying. But that's not all. We are currently going down
with each deployment. And that's not good once we have users, as it will break their experience. So, in the next lesson, we will add Zero-Downtime Deployment to our deployment process.
No comments yet…