This episode is for members only

Sign up to access "Custom User Subdomains in Laravel" right now.

Get started
Already a member? Sign in to continue
09. Deploying


Okay, let's go ahead and deploy this now Of course, this is going to vary depending on who you host with and how you manage your DNS But you should get a general idea of how you're going to go ahead and get this working in production So for the purpose of this we're going to be using Laravel forge to create the server
Which I've already done and create the site within the server, which is the most important part To manage the DNS we're using Cloudflare here so I already have a domain setup here which of course isn't working at the moment and the Name servers for this are pointed over to Cloudflare
Now if you don't have Cloudflare in the middle and you just want to manage the DNS on the domain name that you've registered You won't need to come into something like Cloudflare to do this. You can just do everything within there We're not gonna be doing anything else in Cloudflare except Managing the DNS
Okay, let's get started on this So obviously the first step is that I have the domain and the name servers are pointing over to Cloudflare If you are using Cloudflare, you're gonna want to do that First of all, and of course, you're gonna want to create a site within Cloudflare as well
So we're gonna hop straight over to forge. I've already created a server here I have also gone ahead and created the database already So when we get that set up that's gonna be ready to go and I haven't created a site here just yet And I've deleted really importantly the default site for this as well
So to create a site we're gonna go ahead and choose the root domain. So that is going to be mastering This is just a domain I have hanging around We're gonna go ahead and choose general PHP Laravel. The web directory here is going to be public really importantly Of course, we're gonna click to allow wildcard subdomains really really important
We're not gonna choose create database because I've already created one and we're gonna hit add that's gonna create our site for us Okay, so we're gonna give this a couple of seconds to finish and there we go So I already have a git repository over on github. So I'm gonna go ahead and hook this up now That's just under my username and Laravel user
Subdomain so we're gonna go ahead and install this on the main branch and we are going to install composer dependencies for this Of course because we want to do that when we deploy. So let's click install repository and just let that finish up Okay, now that's done. Let's come straight over to the deployment config We want to go ahead and run an npm install and an npm run build for production on this
So let's go ahead and update our deploy script It's pretty much everything we need to do here and over in environment We're gonna go ahead and copy over our environment from our project just here and then just tweak a few things So let's just paste everything in here
The first thing that we're gonna do really importantly for production is set app debug to false We could always tweak that if we have any issues Another really important thing because within the root level we've defined we use the app URL So we're gonna want this to match up. Of course, we would do this. Anyway with the domain that we're using
So HTTPS mastering Laravel comm we're gonna want to change over the database connection settings as well So we've got the database created the username here It's forged and I'm just gonna go ahead and paste in the password for the database for this server Everything else looks good. And that's pretty much everything else we need to do. We're not using anything else in here
so let's save this out and we're gonna go back over to the app section when that is saved and We're going to deploy this. So let's hit deploy now wait for this to run through and we should be good Okay, that looks like it was deployed successfully Let's come over to mastering Laravel comm and sure enough we have our app in here
Now, of course the database should be empty of users because we've just deployed this So let's go ahead and register out an account here on production and do what we were doing during local development and set a subdomain For this particular user. So let's go over to our profile section Let's go and choose Alex as the subdomain and we should be good
Now if we come over to Alex dot mastering Laravel comm notice that this doesn't quite work at the moment Now the first thing that we want to do the really important a really important thing to do is set up SSL So we're gonna do this with let's encrypt so let's come down to let's encrypt really importantly here We want to make sure that we are applying this to subdomains as well
So we want to make sure this is in here and we're gonna go ahead and use Cloudflare here Now, of course, depending on how you have set things up This will be different but we're gonna go ahead and grab a Cloudflare API token to set up SSL for
First of all our main domain and second of all all of our subdomains as well. So over in Cloudflare we're gonna come over to the overview section just here and we're gonna go down to where our API tokens are so get your API token. We're gonna go ahead and create our token. I've actually already created one here so let's delete that first of all and
Let's go ahead and create this out now for this to work. We need to go ahead and create a custom token We're gonna give this a name. So I'm just gonna call this Laravel user Subdomains now the permissions that we need are zone and
If we come down here to DNS, we need to be able to read or edit the DNS and we also need under zone and zone itself We need to be able to read so they're the permissions that we need to get this working
Let's go ahead and continue to the summary here create the token out Copy this over and we should be able to now just paste that in here and that should work So let's click obtain certificate and just wait for that to install and then we should have SSL Enabled for this particular site. Okay, so that is now done and we now have wildcard SSL set up for this site
Let's try and go to and you can see this still isn't working This is where our DNS settings come in So what we're gonna do here depending on how you're managing DNS is we're gonna add an a record with an asterisk And we're gonna point this to the same IP address as our forge server or forge created server
So let's go ahead and do that and hit save and we're just gonna wait for this to finish up So let's go back over to here and give this a refresh. It's still not working We're just gonna wait a couple of minutes for this to start working and we should be good You might also find that your browser has cached this
so what we could do just to try this out is go back over to our dashboard and maybe change this over to Something completely different. So let's say Alex Garrett Smith and then go over to And you can see yeah, that was probably the case So this is now working and you can see that we now have this subdomain for this user working
Fully protected with a certificate and we are good So obviously this is very specific to using Laravel Forge and Cloudflare But hopefully this has given you a good idea as to how to deploy this if you're using both of these services If you're not that's the general idea. So you should be able to figure it out based on what you're using
9 episodes 31 mins


Allow users to choose their own subdomain, and give them their own area of your application where only their details are shown. You'll be able to register multiple routes for user areas.

Everything in this short course can be easily applied to your own applications where you need this functionality. We'll cover every step of the way, and some things to watch out for.

Finally, we'll learn how to deploy this and configure nginx and DNS records so you're ready to go.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!


No comments, yet. Be the first to leave a comment.