When developing your own project, after making any changes to it, it becomes necessary to deploy it. This is because the changes that have been made require updating the code on the server to ensure that they are available to users.
What is a project deployment?
Project deployment is the process of deploying applications or websites on a server in order to make them available to users. This process includes several steps such as:
- Updating the code: getting the latest version of the code from the repository, for example, using the
git pull
command. - Installing dependencies: installing the necessary libraries and packages, for example, using
composer
ornpm
. - Performing migrations: Applying changes to the database structure using the appropriate commands for migrations.
- Caching: Optimize configuration, routes, and events to improve application performance.
- Restarting the server: Sometimes it may be necessary to restart the service or application to fully apply the changes.
Thus, the deployment process helps to move from the development stage to the current version of the application, which becomes available to users.
A step-by-step algorithm for the process of quickly updating the project on the server
In order to prepare our project for updating on the server, you need to create a filedeploy.sh
in the root of the project on the local server. This file will contain a bash script, the execution of which on an external server will deploy our project. The script will include executing the git pull
command to get all the changes from GitHub and other necessary operations.
Example of the contents of the filedeploy.sh
:
#!/bin/bash
# Go to the project directory
cd /path/to/your/project
# Performing a git pull to update the code
git pull origin master
# (Optional) We install dependencies if we use, for example, npm or composer
# npm install
# composer install
# (Optional) We execute commands to build the project, if required
# npm run build
# Restart the server if required (for example, using systemctl or pm2)
# sudo systemctl restart service_name
# pm2 restart application name
echo "Deployment completed!"
An example from a real project:
#!/bin/bash
set -e
git pull origin master
php8.2 artisan down
php8.2 composer.phar install --no-dev --optimize-autoloader
php8.2 artisan migrate --force
php8.2 artisan config:cache
php8.2 artisan event:cache
php8.2 artisan route:cache
php8.2 artisan up
After the filedeploy.sh
has been created, you need to run the following commands in the console of our project on the local server:
git add .
git commit -m "Change 1"
git push origin master
(if you work in the master branch)
Next, you need to connect to an external server (hosting) using the command:
ssh -i ~/.ssh/id_rsa username@server_address
P.S. The ssh -i ~/.ssh/id_rsa
command is used to connect to a remote server using the specified key file for authentication. The key file is usually generated on our local machine and must have proper access rights. Learn more here.
After we have connected to the server console, we need to go to the folder of our project and run the bash script we created deploy.sh
. This can be done using the following command:
sh deploy.sh
Executing this script will perform all the steps specified in it, which will ensure a quick update of the project on the server. As a result, users will be able to access the latest changes that have been made to the code.