How To Use Operative Bash to Deploy a Laravel Application with Nginx on Ubuntu 24.04
Setting up a Laravel application with Nginx on Ubuntu using Let’s Encrypt Certbot is easy. But, I make it more easier with Operative Bash.
What’s Operative Bash?
Operative bash is a collection of bash script that will install necessary software to run your Laravel Application and NodeJS Application to Ubuntu server. If you run the following code, it will install PHP, Nginx, Node (with NVM), Nginx, MySQL, and other software that you will need for your application setup. It will setup swapfile, create a new user call operative etc..
wget https://raw.githubusercontent.com/setkyar/operative-bash/master/operative.sh
chmod +x ./operative.sh
sudo ./operative.sh "{{ YOUR_LOCAL_SSH_PUBLIC_KEY }}"
Before we do that, I think it’s good idea for you to read what we are about to install by reading the Operative’s bash script.
Setting up Server in Digital Ocean
To get started, we will log in to Digital Ocean and create a droplet. Visit – https://cloud.digitalocean.com/droplets/new and choose region. For me I choose Singapore.
Next, we will choose an image and version. In this case, we’ll select Ubuntu 24.04 (LTS).
For the droplet size, I will choose the $7/month plan with 1GB RAM, 1 AMD CPU, 25GB NVMe SSD storage, and 1000 GB of transfer.
We won’t choose any additional storage.Next, I will use an SSH key that I created. (If you don’t have one, you can create your own to avoid needing a password to log in to the server.). I will also enable ‘Add improved metrics monitoring and alerting (free)’ as it’s a free and easy way to monitor the server. Finally, I will create the droplet. I won’t configure a hostname or other settings, but you may want to do so.
You’ll need to wait a few seconds for the droplet to be created and assigned an IP address, as shown below.
After obtaining the public IP address, it’s a good idea to point your domain to this IP, as we’ll be adding an SSL certificate using Operative Bash. I’m using Cloudflare for my DNS, so I’ll update my DNS records there. If you’re using a different DNS provider, you’ll need to update your records with that service.
Setting up server with Operative Bash
Now it’s time to SSH into the server and set it up. I ssh into the server using the following command
$ ssh [email protected]
Now, I am inside the server with root
user. I will download the operative bash using the following commands
$ wget https://raw.githubusercontent.com/setkyar/operative-bash/master/operative.sh
$ chmod +x ./operative.sh
First, it download using wget, then it make it executable by running chmod +x. Now, it’s time to run operative.sh. When you run you have to provide your public key with quote “. Like the following
./operative.sh "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDER+t+pCERmQtr1z/nM5CRPZKeEF66oyF/MGdFpqC6jCUTX8DV31+OEeU7Pn76ulP4mu0Ey0P+0D3QVgiD7AB9BxVbfcH2cRbSt3zPebQtZ9nHT0+sp3kDz6jVi/rbsj2KwpEmFO/iPm+lIQ70sbx2Kz27qLlNv1NZJLrgid3VdNa+SDRU2OXUsGtDgxgxv1NwqBpKU7vq0T0rOpp1AOC2n1CllLgAckrsp7Hp1YU00xo1Xjdk0ZcaJC08FPPGoeSwgqLt+X+jEQeEVyhXMhQl6g4SI0nYfDh0Nh2pHFGzayxZnAoE3h+i+XQiTt9YDkkeqIUGQYyCmm6l0c7YQTtL [email protected]"
You can get public ssh key from ~/.ssh/id_rsa.pub
if you are on mac or linux. When you run the above command it will ask you to provide the PHP version. I will choose 8.3 in my case. After that, it will ask you to choose the NodeJS version. I will choose v20.18.0.
It will install all the software that require to run PHP/Laravel Application. You can see the outputs as well. Sometime, it might ask you to answer some basic questions so definitely checkout the output wile you are waiting for it to finish.
When the script finishes, you’ll see a message like the following…
You can now login to the operative user with the following command:
ssh [email protected]
You can get the MySQL root password and operative user's password from the output.txt file
You can find the MySQL root password and the operative user’s sudo password in the output.txt
file. In my case, it looks like this:
The generated password for 'operative' is: DMVhikNS/w5Pz+8v
MySQL root password: KkHnQZ0crkERq6tp
Now that our server is ready, it’s time to set up our website.
Setting up Laravel (site)
I open a new terminal and login with operative user.
$ ssh [email protected]
Since we provided the public SSH key, it will use that key for authentication. Now to add new Laravel site, get the site.sh from operative-bash repo like the following…
$ wget https://raw.githubusercontent.com/setkyar/operative-bash/master/site.sh
$ chmod +x site.sh
As always, I recommend that you read through the script. Basically, it’s getting Nginx template that I prepare and replace with your domain.
Now it’s time to set up the Laravel site. In this example, I’m using a public repository. In your case, if you want to use private repo, you will have to get the public key from the server and add it inside your Github account. You can get your server’s public key from $ cat /home/operative/.ssh/id_rsa.pub
Once everything is ready, run the following command to set up the site. Be sure to replace server name
with your actual server name and repo
with the URL of the repository you want to deploy.
$./site.sh operateservers.com "https://github.com/setkyar/operative-laravel-simple-v2.git" --laravel
The script will ask for your sudo password to set file permissions and will also ask if you want to add a Let’s Encrypt SSL certificate using Certbot. Answer ‘yes’ to this prompt. You’ll then be asked to provide some information for the certificate.
If you encounter any issues at this stage, you can clean up the configuration by running the following commands:
sudo rm -rf /etc/nginx/sites-enabled/your-server-domain
sudo rm -rf /var/www/your-server-domain
Of course, you will have to update your-server-domain with your own domain.
If you visit your domain now, you should see something similar to the following:
This is because we haven’t yet updated the .env
file. The .env
file is located within our repository, and in this case, it’s at /var/www/operateservers.com/.env
.
Now, open the .env
file with vim
or nano
and update the following settings:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=KkHnQZ0crkERq6tp
We’ll be using MySQL, and our database will be named ‘laravel’, which we haven’t created yet. The username is ‘root’, and the password is the one we retrieved from output.txt
.
Now it’s time to set up the database and run the migrations. Run the following command to create a new database:
$ mysql -uroot -p
# type mysql password when it request
Next, run the following command to create the ‘laravel’ database:
mysql> create database laravel;
Query OK, 1 row affected (0.01 sec)
mysql> exit
Bye
Now, run the database migrations by executing php artisan migrate
. You should see the following output:
Finally, visit your domain in your web browser, and you should see the Laravel welcome page.
That’s it! Your domain now has SSL enabled. You can add additional subdomains by repeating the ‘Setting up Laravel (site)’ steps. If you have any questions or get confused, check out the video and feel free to leave a comment below!”