This is a third part of the Deploy Laravel 5.4 App on LEMP Stack (DigitalOcean Guide). In this post, I'm going to prepare the server for Laravel application deployment.
Let's create a folder for our Laravel application. By default, NginX will look at the /var/www/ path to serve the files. In this folder let's create a folder called app.
Open the following file to update the new path. Run the following command in your terminal.
sudo nano /etc/nginx/sites-available/default
Let's change the root line and add the public folder path.
Change the following location section to add /index.php?$query_string;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
Changed code section should look like this.
Restart the server to changes to take effect.
sudo service nginx restart
You can follow the official guide to install composer. In short, execute the following commands in your terminal.
cd ~
curl -sS https://getcomposer.org/installer | php
We have composer.phar in our home folder move it into our bin so we can use composer commands easier by just typing composer.
sudo mv composer.phar /usr/local/bin/composer
Type composer in your terminal and you will see the output of composer help files.
Using git, you can push the source code to our server effortlessly with no downtime. You can use SFTP as well, but it is a slow and less secure approach. In this tutorial, I use git to upload the source code to the server.
Let's install Git in /var/repo/ folder. Run the following commands in your terminal.
cd /var
mkdir repo && cd repo
Execute the following commands. --bare flag is generally used on servers. A bare repo is a special kind of repo whose sole purpose is to receive pushes from developers.
mkdir site.git && cd site.git
git init --bare
Above commands creates the repository in the /var/repo/site.git file path.
Git has a way to fire off custom scripts when certain actions occur. There are two groups of these hooks: client-side and server-side. Client-side hooks are triggered by operations such as committing and merging, while server-side hooks run on network operations such as receiving pushed commits. In this tutorial, I'm going to focus on server-side hooks.
There are three types of hooks. They are pre-receive, post-receive, and update. You can learn more about them using this link
We focus on post-receive because we need to trigger it after the source code push. According to the documentation, post-receive
To set up hooks go to the hooks folder in site.git.
You will see sample files inside the hooks/ folder. But in this tutorial, I'm going to create a new empty file called post-receive. Run the following command to create an empty file.
sudo nano post-receive
Add the following code to the file created. Save the file and exit.
#!/bin/sh
git --work-tree=/var/www/app --git-dir=/var/repo/site.git checkout -f
The post-receive file needs execution permissions in order to work. Run the following command to add execution permissions to the file. Please note that you are still in the same folder.
sudo chmod +x post-receive
Now you can exit from the SSH session. Because in the next step we are going to work on the local machine. You can use exit command to exit from the ssh session in the terminal. Next blog post will focus on how to set up local machine to push the production code.
Let's create a folder for our Laravel application. By default, NginX will look at the /var/www/ path to serve the files. In this folder let's create a folder called app.
Open the following file to update the new path. Run the following command in your terminal.
sudo nano /etc/nginx/sites-available/default
Let's change the root line and add the public folder path.
Change the following location section to add /index.php?$query_string;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
Changed code section should look like this.
Restart the server to changes to take effect.
sudo service nginx restart
Install Composer
You can follow the official guide to install composer. In short, execute the following commands in your terminal.
cd ~
curl -sS https://getcomposer.org/installer | php
We have composer.phar in our home folder move it into our bin so we can use composer commands easier by just typing composer.
sudo mv composer.phar /usr/local/bin/composer
Type composer in your terminal and you will see the output of composer help files.
Install Git
Using git, you can push the source code to our server effortlessly with no downtime. You can use SFTP as well, but it is a slow and less secure approach. In this tutorial, I use git to upload the source code to the server.
Let's install Git in /var/repo/ folder. Run the following commands in your terminal.
cd /var
mkdir repo && cd repo
Execute the following commands. --bare flag is generally used on servers. A bare repo is a special kind of repo whose sole purpose is to receive pushes from developers.
mkdir site.git && cd site.git
git init --bare
Above commands creates the repository in the /var/repo/site.git file path.
Git Hooks
Git has a way to fire off custom scripts when certain actions occur. There are two groups of these hooks: client-side and server-side. Client-side hooks are triggered by operations such as committing and merging, while server-side hooks run on network operations such as receiving pushed commits. In this tutorial, I'm going to focus on server-side hooks.
There are three types of hooks. They are pre-receive, post-receive, and update. You can learn more about them using this link
We focus on post-receive because we need to trigger it after the source code push. According to the documentation, post-receive
hook runs after the entire process is completed and can be used to update other services or notify users.To set up hooks go to the hooks folder in site.git.
You will see sample files inside the hooks/ folder. But in this tutorial, I'm going to create a new empty file called post-receive. Run the following command to create an empty file.
sudo nano post-receive
Add the following code to the file created. Save the file and exit.
#!/bin/sh
git --work-tree=/var/www/app --git-dir=/var/repo/site.git checkout -f
The post-receive file needs execution permissions in order to work. Run the following command to add execution permissions to the file. Please note that you are still in the same folder.
sudo chmod +x post-receive
Now you can exit from the SSH session. Because in the next step we are going to work on the local machine. You can use exit command to exit from the ssh session in the terminal. Next blog post will focus on how to set up local machine to push the production code.
Comments
Post a Comment