Creating GitHub Deploy Key0:00
Hey there, so right now we have Nginx, php, php-fpm, and Composer installed. It's time to pull our Laravel application from GitHub, install it on the server, and configure Nginx to run it. And to do that we need to generate an SSH key pair on the server, copy the public key, and save it as a deployment key on GitHub. That way our server can authenticate into the repo on GitHub and pull the application files. So let's start with that. We will run ssh-keygen, provide the filename, home, open to ssh/id_rsa, the type is an rsa key, and provide a blank passphrase.
We will run ssh-keygen, provide the filename, home, opento, ssh, id, rsa, the type is an rsa key, and provide a blank passphrase. Now let's print the key, cat, home, opento, ssh, id, rsa.pub, that's the public key. Copy it, then go to our repo on GitHub, go to the settings tab, then deploy keys, let's add a new key, name laracasts, then paste the public key. And that's done. Now let's go back to the terminal and check the var directory. The www directory here is where Nginx expects sites to be stored by default. Notice here that this directory is owned by the root user and belongs to the root group. This means if we want to write any files in this directory we will have to use sudo.
Cloning Repo to Server1:36
Notice here that this directory is owned by the root user and belongs to the root group. This means if we want to write any files in this directory we will have to use sudo. So to make things easier for us, let's change the owner of this directory to opento. So we run sudo chown for changing owner, opento for the user and opento for the group as well. Then provide the directory name. Now we as the opento user own this directory. Let's cd inside and run git clone, provide the repo information and execute the command. The operating system asks us if we want to add github.com as a known trusted host. We say yes of course. And here we go, the repo is installed.
Installing Dependencies and Env2:33
We say yes of course. And here we go, the repo is installed. Let's go inside and run composer install. Wait for the command to finish. Then check the directory. And here we go, the vendor directory is there, meaning our composer dependencies were installed. Now we need to create the .env file. So let's copy the example file that comes with Laravel, create a new .env file on the dbs, paste the content and then save. One final step here is to generate an application key.
Preparing Nginx for Laravel3:09
dbs, paste the content and then save. One final step here is to generate an application key. So we run php artisan key:generate. Now if we go visit the IP address in the browser again, we still see the default nginx site. We need to tell nginx about our Laravel application. So let's go back to the terminal. And before we configure nginx to display our Laravel application, we need to change the ownership of the www directory to a user and a group called www-data. I'll tell you why in a bit. But for now, let's run sudo chown www-data:www-data for the www directory.
Writing Nginx Server Block3:55
I'll tell you why in a bit. But for now, let's run sudo clone wwwdata wwwdata for the www directory. And now this directory is owned by the wwwdata user and belongs to the wwwdata group. Let's go configure nginx at /etc/nginx/sites-available. We have only one file in this directory. So let's create a new one, so sudo nano laracast. I'm going to paste an nginx configuration block that I usually use for new applications. Let's scroll back up and explore this nginx block. So this block configures nginx to listen on port 80 and consider this site the default one if nothing else matches.
Then we tell nginx that our index file could be index.html, index.htm, or index.php. Then we add a location block that matches any URI. And tell nginx to try opening a file with the same name as the URI, if not then try a directory with the same name, if not then pass the request to an index.php file along with the query string. Then we add another block, this one handles all php files and forwards the request to php-fpm through the common communication socket set by the fastcgi_pass attribute here. Let's now save the file. And the final thing you need to know is that nginx will only read blocks that are enabled. And the way to do that is to create a symbolic link between our block file that's inside
Explaining www-data Ownership6:22
And the final thing you need to know is that nginx will only read blocks that are enabled. And the way to do that is to create a symbolic link between our block file that's inside the sites-available directory into the sites-enabled directory. So here I'm going to run this command, which will create the symlink we need. And that's it. So now let's talk about why we had to change the ownership of the www directory to www-data. This www-data user is special. Nginx uses it to access public files in the public directory of the Laravel application. It is also used by php-fpm to execute the php code.
Nginx uses it to access public files in the public directory of the Laravel application. It is also used by php-fpm to execute the php code. Nginx doesn't need write access to any files on the site, but php-fpm does. Because the Laravel application needs to store several cache and log files in the system. If you are familiar with Laravel, you know that it needs to write some files. So to speed things up and get something up and running really quick, we just made the www-data user the owner of the www directory, so both Nginx and php-fpm can access the files, and for php-fpm to be able to write files to this directory. Let's go back to the terminal and continue. Inside the sites-available directory of the Nginx configuration file, we still have the
Enabling Site and Reloading7:50
Let's go back to the terminal and continue. Inside the sites-available directory of the nginx configuration file, we still have the default site in place. nginx won't be able to run with 2 default sites. So let's delete this one, run rm default, oh we need sudo, so sudo rm default. Then go to the sites-enabled directory, delete the default symlink as well. And now let's reload nginx, visit the IP address in the browser. And here we go, our Laravel application is up and running. Let's do a quick recap of what we did so far. We generated an ssh key on the server and added it to the github repo.
Let's do a quick recap of what we did so far. We generated an ssh key on the server and added it to the github repo. We cloned the repo on our server and installed the application dependencies via composer. We made the www-data user the owner of the www directory where our application lives. And finally, we configured nginx to serve our Laravel application for all requests coming to the server through its ip address. Next lesson, we will take some measures to easily and securely run several applications on our server. We will see you then.
