Hope you have already created and EC2 server and connected via putty. if not this tutorial you can found how to do it.
Deploy Laravel application with Ubuntu Nginx in EC2 server (You don't need to check full tutorial just server creation and connect is enough)
Install Nginx
sudo apt update
sudo apt install nginx -y
sudo systemctl status nginx
PHP
sudo apt install -y software-properties-common
sudo add-apt-repository ppa:ondrej/php -y
sudo apt update
sudo apt install -y php8.1 php8.1-fpm php8.1-mbstring php8.1-xml php8.1-mysql php8.1-curl php8.1-zip
Install node
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt-get install -y nodejs
node -v
npm -v
Clone the project with git
cd /var/www/html
sudo mkdir project
Install composer
sudo curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
sudo chmod +x /usr/local/bin/composer
now go inside the folder and clone the repository
cd /var/www/html/project
But you will see you will be asked for a password
We have to do this to get an app password
- Log in to Bitbucket: Go to Bitbucket and sign in with your account.
- Access Personal Settings: Click on your profile icon in the bottom left corner, then click "Personal settings".
- App Passwords: Navigate to "Access Management" > "App passwords" in the left sidebar.
- Create App Password: Click on the "Create app password" button.
- Select Permissions: Give the app password the necessary permissions for your tasks. For Git operations, you typically need "Repositories" read and/or write access.
- Create: Name your app password and click "Create".
- Copy the Password: Make sure to copy the app password and store it securely; you won’t be able to see it again.
But there is a n important point here. When you are going to pull the repo you will see this,
They ask app password for username 'vimuths'. We can avoid this by removing username from clone url. That way any user will be able to clone with there user name and password
git clone https://bitbucket.org/yourgit/rourrepo.git .
Here we have a tricky part. Think you have noticed git clone command is asking for a password here. This gives errors for pipelines. We can avoid it like this
cd /var/www/html/project/
sudo vi .git/config
Now edit this line...
[remote "origin"]
url = https://bitbucket.org/yourgit/yourrepo.git
To (Add username and app password)
[remote "origin"]
url = https://username:app_password@bitbucket.org/yourgit/yourrepo.git
Now when you do a 'sudo git pull' there will not be any prompt to input username and password
now add this file to root
bitbucket-pipelines.yml
image: atlassian/default-image:2
pipelines:
branches:
main:
- step:
name: Deploy and Migrate
script:
- pipe: atlassian/ssh-run:0.2.8
variables:
SSH_USER: 'ubuntu'
SERVER: 'your_ip'
COMMAND: 'sudo chown -R ubuntu:ubuntu /var/www/html/project && git config --global --add safe.directory /var/www/html/project && cd /var/www/html/project && sudo git pull origin main && sudo composer install --no-interaction && sudo chown -R www-data:www-data /var/www/html/project'
Make sure to replace *your_ip * with your actual server ip address
Then you need to enable pipelines inside your repository. This section is inside repository settings
Then you need to create a public key and private key (.pem) using puttygen. And insert them there.
And don't forget to add the server ip to known hosts.
Now SSH into server again and add these commands,
cd /var/www/html/project/
sudo composer install
sudo chmod -R 775 /var/www/html/project/storage
sudo cp .env.example .env
sudo php artisan key:generate
Now if you do a git commit from your local machine you will see pipelines run successfully.
Now let's add .sh file or shell script to make this more complete. create .deploy.sh file inside root.
#!/bin/bash
# Change ownership to ubuntu
sudo chown -R ubuntu:ubuntu /var/www/html/project
# Mark the repository as a safe directory
git config --global --add safe.directory /var/www/html/project
# Change to the project directory
cd /var/www/html/project
# Pull the latest changes
sudo git pull origin main
# Install dependencies without dev dependencies and without interaction
sudo composer install --no-interaction
# Change ownership back to www-data for the web server
sudo chown -R www-data:www-data /var/www/html/project
And call him here in pipelines file
COMMAND: 'cd /var/www/html/project && ./deploy.sh'
And run this in cli in server.
sudo chmod +x /var/www/html/project/deploy.sh
Top comments (0)