Deploy Laravel on Ubuntu Apache server

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
let's look at how to deploy a laravel application on a Ubuntu Server running Apache to do this of course you're going to need a laravel application here's the example I'm working with I've got it running locally I just took a Bare Bones layerable application customize the home page I did add a single route within there just so we can test the routing system once we get things deployed and the code for base for this I have on a repository on github.com this is how we're going to get it to the server we're going to clone it from this Repository in terms of the server it should of course be running Apache and you should have SSH access to it the way that I'm going to get access is I'm connecting via my code editor vs code I'm using their SSH plugin so right now this window is directly connected to my server so anything I do here whether it be via command line or the file browser is going to be happening on the server itself alternatively you could disconnect your server via regular SSH via command line run the commands there if that would work as well as part of the setup process I'm also going to be showing how to configure a domain or subdomain to point to your laravel application on your server in order to do this you do need to pre-configure The Domain you want to use to point to the IP address of your server and just to show what that looks like switching over this is namecheap.com this is where I have my domains registered I'm within my DNS settings and I went ahead and set up a subdomain for this demo so I've got the subdomain of demo this is going to be on this domain code with susan.com and it's pointing to the IP address of the server that I'm working with I'm using a subdomain here just because I don't want to interfere with the content of my main domain but you could set this up with a primary domain a subdomain however you want to set it up just as long as that domain is pointing to the IP address of your server just to show that this is working I'm going to go to demo dot code with susan.com and right now it's just showing the default splash page we get with digitalocean servers when setting up a Linux Apache based server so by the time we're done with this this should be pointing to our laravel application the final prerequisite is your web server is going to need to be able to communicate with GitHub so it needs to authenticate that communication and I recommend setting this up with SSH keys if this is something you've not already set up I do have a separate guide that goes over the details of doing that but if you have done it or if you're not sure if you've done it the way you can confirm it is while SSH into your server run the command SSH T get github.com and if it reports back high whatever your GitHub username is you've successfully authenticated you know you have your SSH Keys set up if not like I said go through this guide and you can get that set up so with all those prerequisites behind us let's jump in the first thing we need to do is just make sure that our server is prepared to run a layerable application and for this we're going to reference the laravel docs on server requirements and Within These docs we can see that as of this recording the minimum requirement of PHP for layerable is 8.1 and here's a list of PHP extensions we're going to need on our server now if you're watching this video in the future you might see different version numbers here you might even see different PHP extensions so whatever I'm about to say just apply it to what you're currently seeing in the documentation so digging into this let's first check our PHP version and the first check I'm going to do is just via command line I'm going to invoke PHP with the version flag and you can see it's reporting back 8.2 so I'm good to go with that 8.1 minimum requirement but this is specific to just PHP from command line I need to also make sure that the PHP module that Apache is using is the appropriate version so for this I'm going to create a info page that's going to Output a bunch of information about my web server to the brow browser so we could double check that where I'm going to create this is in my main document root which in this server it's Inver www.html so I'm going to move into that directory and within here I will create a file just call it info.php and then within this file in PHP I'm just going to invoke the built-in PHP info function and then pull this up in my browser and it looks like we're good to go also running 8.2.6 here meeting that 8.1 minimum requirement now in your case if you find that you need to upgrade a PHP to work with laravel I do have a separate guide on that you can find a link in the notes that accompanied this video also include a link in the video description but assuming you're good with that the next thing we want to check from the server requirements is we want to make sure we have all these PHP extensions set up on our server way we're going to do that is we're going to invoke the command PHP Dash M to Output all of our currently installed PHP extensions so coming back to command line I'm going to say PHP Dash m and what we want to do with these list of extensions is basically cross reference it with the list of extensions that laravel list in their docs and just to save us some time I did that before recording this video and here are the results I found I had most of the extensions they were three I was missing curl extension Dom extension MB string and then XML all right so in my case those are four extensions I need to add to my server and the way that we can do that is using apt-get a common dependency Management program for Linux based operating systems the procedures are quite simple the first thing we need to do is set up a repository where we're going to be getting our modules from this is a very common one we use in the world the PHP so I'm just going to copy this command and run it press enter when it prompts you to do so with that complete the next command we want to run is apt-get update to get the latest packages from our apt repositories and now we can actually get our modules and we're going to do that with this command app to get install and we're going to list all the modules that we need so here in this example you can see I've got that curl module the Dom module MB string and XML those were the four that I was missing for any of these module names you just want to prefix it with the version of PHP that you're currently running on your server and in this example in addition to the four missing modules I'm also going to be adding in the MySQL PHP module just because that's the database type I typically use in my layerable applications if you're not using a database it's not required but just wanted to include this just to note that you might need to do some additional work to get your database up and running I'm also throwing in the module zip and unzip these are two modules that composer are dependency Management program we use in laravel is going to expect if you don't include this you'll get some warnings when you get to working with a composer here in a moment so long story short adapt this command as needed for the modules you're missing on your server and then go ahead and run it and then we can run that PHP Dash M command again just to double check that we're good to go so for example there's that Dom module I was missing before it's now listed there there's MB string uh and then XML and then the fourth one was a curl so we could see that as well so with all our modules in place the next step is we need to make sure that our Apache server has URL rewriting enabled this is going to be essential for laravel's routing system to work to do this we can run this command to enable the rewrite module and then we'll go grab another command to restart our Apache server to make those changes take effect next up we want to make sure our server has composer installed to check to see if you have it installed just try to invoke it if it says command not found you don't have it installed and so coming back to the notes you need to install it first step to do that is move into your systems user bin directory it's a common location to put command line based programs so we'll do that first once we're in that directory we're going to run this curl command to download the composer installer and at this point composer is installed but it is installed with this.phar extension which we don't want to have to type out every time we invoke composer so we're just going to rename that using the move command so say composer.phar to simply just composer all right now to test it out let's invoke composer and in my case I am getting a warning about running composer as the root user because you can see I'm currently logged in as the root or admin user and that's just because this is for demo purposes I created the server to record this video I'm going to be deleting it afterwards I didn't create a separate user to interact with my application and composer which is what you should be doing because with composer you're dealing with outside software you don't want to interact with it as that root admin user just because that root admin user has full privileges over your server and you don't want anything bad to happen there so long story short for security purposes don't do what I'm doing in terms of operating as the root user in my case like I said this is just a demo so I'm going to ignore this just say yes and proceed with working with composer and you can see the results even just invoking composer in general you see some output about how to use composer and this is basically we're looking at looking for at this point is just confirmation that it's actually installed and seeing that it's installed that's the last the last thing we need to do in terms of preparing the server to run a laravel application our next step if we follow along in the notes is to actually get the application one on a deploy we want to get the code base and we're going to get that from GitHub so coming over to my repository on GitHub for this application I'm working with I want to find the SSH address for this repository I'll find this under the green code button make sure that SSH is chosen and just copy this URL and then on my server I want to move to the location where I want to put this application and for this I'm going to go back to that VAR www directory I'm not going to go into HTML that's the default document root on the server I'm going to be creating a separate document route for this application so I'm just going to put it in VAR www so I'm going to move into there and then here I'm going to say git clone and paste in that SSH address and it should pull down that code base to this directory to confirm that it works if we look at our directory contents we should now have a directory named after our repository in my case that's demo so if I move into that and then list the directory contents here you should see all the files and directories you typically see in a laravel application and of course it should mirror what you're seeing when you look at your code base on GitHub so now that I've got my code base I'm going to go ahead and open this in my file explorer just to make it easier to look at the project and work with my files so in vs code I'm going to say open folder and I'm going to open up that verw demo directory and seeing this here let me zoom in on the contents of our project the first thing we should observe is we don't have a vendor directory the vendor directory holds all of our outside dependencies in our layerable application and the vendor directory is managed by composer it's not something that we are typically tracking as part of our code repositories in fact if you look at your git ignore file you can see that the vendor directory is typically listed there this makes sense because it's outside code we don't need to track the development of that code we simply just need to have it and like I said composer is what is going to get that code for us not cloning our project to the server so knowing that let's get composer to bring in our vendor directory I'm going to bring back my terminal window making sure that I'm within the project directory I'm going to invoke composer and there's two possible commands I might run here to get my vendor directory let me go back to the notes to reference this command number one would be if you're working on a production server in other words you're working on the server where real world users are interacting with the application there you would want to run composer install optimize autoloader no Dev this is going to do a few different things one is going to exclude any of your outside dependencies that are just for development purposes things like testing tools we don't need them on production so we would exclude them there it's also going to optimize your autoloader just to make class locations within your application faster so that's going to be good and then composer install that's going to make it so that when it's getting your outside dependencies it's looking at the version numbers of your dependencies as written to your composer.lock file and the lock file is something that is written when you're working on your application in development it's going to record the specific version numbers of the outside dependencies you're working with and so by running composer install on the production server it's going to get the version numbers from composer.lock ensuring that all of the dependencies you're using in production exactly match the dependencies you were using in development so everything's consistent now contrast that with composer update this command you might run if you're working on a development server because composer update rather than reading from your lock file it's going to read from your just regular composer.json file where you list all of your dependencies and it's going to pull in the latest version of those dependencies within the version constraints that you set and after it does that it writes the specific versions that it got to the lock file so that when you are operating on production and you're running composer install again it's going to grab the exact versions that were set up in development so that everything remains consistent so that's a bit of a tangent about composure in general long story short if this is a production server you're setting up go with this command if you're working on a development server go with this command for this example I'm going to treat what I'm working on as a production server so I am going to use the composer install command so let me run this and again just make sure you're in your application when you run these composer commands we're going to say yes again just ignoring that warning you can see composer pulling in all of my outside dependencies and with that complete I now have my vendor directory another important piece of content within our application that is currently missing because it's ignored by git is our environment file the environment file contains configurations that are specific to the different environments or contacts in which you run your application and so it's not something you keep synchronized with your entire code base therefore you have to manually create it whenever you're setting up your application in a new context and the way I'm going to create it is every layerable application has a DOT env.example file we can use this as like a starting template for our environment file so I'm just going to create a duplicate copy of that I'll just do it via command line so we're going to say copy env.example2.env all right then opening this up uh there's a bunch of different configs in here but the most important ones we need to address are just this top chunk here all of our app configs we can go ahead and change our app name in my case I'm just going to call it demo we can set our environment uh common environments you might see our local production staging testing in this case I am treating this as a production server so I'm going to call this the production environment I'll come back to app key in a second we're going to need that for app debug typically on a production environment we would set debugging to false this is going to make it so if there are errors on our application the user is going to see a generic error page rather than like full details about the error because I am setting this application up for the first time though I am going to leave this to true just in case when I run this application initially if there's any problems I could see full debugging information once things are running smoothly though I would want to come back and change this to false on a production context but this is not technically real world yet I don't have real world users using it so I'm okay with debugging information being output for the app URL to set up whatever domain or subdomain you're using so again in my case that was demo codewithsusan.com all right and you could skim through the other settings here uh you can leave most of them as the default unless there's something you've specifically worked with on this application for example if you've set up redis you would want to go in and put the appropriate configs there if you have a mail server you want to work with you would want to put the configs there but for the spirit bonus example this is all I need to change to get this up and running now let's address the app key I'm not going to manually enter it instead I'm going to invoke Artisan uh laravel's built-in command line utility and have it generated for me so that's going to be PHP Artisan key generate all right you can see it filled it in with an encrypted value this is going to be used for security and encryption within my laravel application that should be all I need to do in the environment file for right now so let's check back in on the notes and see our next step which is to set some permissions specifically our storage and our bootstrap cache directory these need to be writable by our web server because when we're running our application it's going to write contents to those directories things like cache info session data logging Etc to set the permissions on this we need to figure out which user on our system our web server is running as and I've got a command that's going to do that for us it's basically going to look at our running processes search for Apache and tell us what the user is so we can run this as written in the notes and you can see in this server the username it returns is www.data which is a common username that is often set up on servers like this all right so now that we know that we're going to run to change ownership commands and we're going to change the owner to be in my case www data you would want to swap this out with whatever you got from the previous step and then the name of the directory you want to change the ownership of so we're going to do this first for storage and then bootstrap cache next and with our permissions all set that brings us to our final step which is configuring the site on the server in other words we need to make it so that when we go to our domain or subdomain it's actually going to point to this application the way we're going to do that is creating a site config in our Etc Apache 2 sites available directory so in my code editor I can create a new file using the code command I'm going to specify that directory and then you can call the config file whatever you want in my case I'm going to name it after the application so I'm going to say demo dot config or conf for short common convention you'll see with these config files all right so that creates a new empty file waiting for my config I'm just hovering over it checking that the path is as expected and the configs I'm going to put in here I'm going to borrow from this template in the notes we've got a virtual host config I'm going to paste that in and we just want to go through and make changes as appropriate here so starting with the server name what is the domain or subdomain you want to load the site the way I'm setting this up it's demo code with susan.com so I'm going to set that the document root should point to the directory of your application specifically the public subdirectory right so that's this directory here whenever you're running a layerable application you're configuring your web server it always needs to point to that public subdirectory that's the only directory that should be accessible by the web browser when we go to the server it's going to point to that directory it should load this index file and that index file is set up to bootstrap your entire application and it would it will communicate with the rest of the code here but the web browser itself should always just point to that public subdirectory all right so set that as needed for your document root use the same path for this directory options here here's where we're setting some necessary options for that document root and you can leave it as the defaults I have here in this template and then the final thing down here is just some file names for things like your error logs and your access logs so you can change this to whatever the name of your application is in my case I'll just leave it as demo all right so with that uh config in place and saved I just need to enable it and I'm going to do that with this command two AE to enable site and then the name of the config file so I can copy this exactly as written of course on your case you would want to update demo.config with whatever file name you used and then after it's enabled I like to run this command Apache to control Dash T this is just going to check and make sure that there's no errors in your configs so I'll just run that you will see this message about uh determining the server's fully qualified domain name that's fine you can ignore that any other messages you see here are beyond what I'm showing here would indicate that there's some problems in your config so just read through that address any problems that it reports but assuming everything is okay we're going to do one final restart on Apache with our system control restart Apache 2 command and we should be ready to test this out so we should be able to go to demo code with susan.com it should point to our demo directory specifically that public subdirectory and load up our application let's see if that's the case perfect there's that same homepage we're seeing when we're running this application on our local server we're not seeing any sort of Errors we can also test that our routing system is working correctly so if I go to my test route I had set up we can see that that's reporting as expected so everything looks good on my end but if you reach this point and it's not working as expected your application's not loading or you're getting an error page or anything like that check out at the bottom of the notes that accompany this video I have a link to another guide on common laravel installation issues that can walk you through troubleshooting why things might not be working as expected you can also leave a comment below describing your particular problem and I can try to point you in the right direction
Info
Channel: Susan B.
Views: 10,320
Rating: undefined out of 5
Keywords:
Id: iye4lmB6diE
Channel Id: undefined
Length: 22min 11sec (1331 seconds)
Published: Fri May 26 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.