Add Laravel project to EC2 server, Ubuntu / Nginx with bitbucket pipelines
Published: 2024-03-19 10:13:46

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](https://dev.to/vimuth7/deploy-laravel-application-with-ubuntu-nginx-in-ec2-server-5c2a) (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
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zo0c5egkwtdfutjyvmjd.png)

But you will see you will be asked for a password

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xoeqj77gq0h3ttuy0eyj.png)

We have to do this to get an app password

1. **Log in to Bitbucket:** Go to Bitbucket and sign in with your account.
2. **Access Personal Settings:** Click on your profile icon in the bottom left corner, then click "Personal settings".
3. **App Passwords:** Navigate to "Access Management" > "App passwords" in the left sidebar.
4. **Create App Password:** Click on the "Create app password" button.
5. **Select Permissions:** Give the app password the necessary permissions for your tasks. For Git operations, you typically need "Repositories" read and/or write access.
6. **Create:** Name your app password and click "Create".
7. **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,
> ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xkzianq6ty7q8rc3zb8d.png)
> 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
> ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wh4fqbspkir6loq9ljzx.png)

```
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**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vbw4ql2nqfdd43isa250.png)

Then you need to create a public key and private key (.pem) using puttygen. And insert them there.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6f5rp5ryd66y1kpl5pwy.png)

And don't forget to add the server ip to known hosts.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/55q48bcg4gmhuqn1n82l.png)

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.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/97ostl5e38s9hxwuft42.png)


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



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0se9uwv8oc3l407iluvd.png)


COMMAND: 'cd /var/www/html/project && ./deploy.sh'

And run this in cli in server.

```
sudo chmod +x /var/www/html/project/deploy.sh
```