Before we proceed to deployment, i assumed you already had an AWS account, if not, you can also register one AWS account for free and use the free tier for one year
Create A Virtual Machine
In the AWS Console, Click EC2 under compute
Once you're signed in to your AWS account, navigate to the EC2 service by clicking on "Services" at the top of the page and typing "EC2" in the search bar.
Click on "Launch Instance" to create an VM
Click on "Launch Instance" to create a new EC2 instance.
Select Ubuntu Server 20.04 (LTS)
Actually, Laravel do not care which distro you are running, so feel free to choose preferred distro of you, but I am gonna just choose Ubuntu here.
Choose Instance type
For composer, it is recommended to have at least 1GB of RAM for
composer install
Go a head and choose the t2.micro/t3.micro for instance type.
Set up misc information
You can set up to 30 GB, as that is maximum aws free tier provide
Network Configuration
Configure the security group. You will need to allow inbound traffic on port 80 (HTTP), 443 (HTTPS) and port 22 (SSH) to be able to access the Laravel application and connect to the instance using SSH.
Review Setting
Review your settings and launch the instance.
Generate Key Pair
When launching the instance, AWS will prompt you to generate a key pair. This is necessary for connecting to the instance using SSH.
Choose "Create a new key pair" and give it a name.
Download the private key file (.pem file) and save it to a secure location.
Note: You will not be able to download the private key file again, so make sure to save it in a safe place.
Setup Instance
Connect to the Instance
For Unix-based systems (Mac, Linux):
Run the following command to change the permissions of the private key file: chmod 400 your_key_pair_name.pem
Run the following command to connect to the instance: ssh -i your_key_pair_name.pem ec2-user@your_public_dns_name
Note: Your public DNS name can be found in the EC2 dashboard under "Instances" -> "Description" -> "Public DNS (IPv4)"
For Windows:
If you're using the default Windows Command Prompt, run the following command to convert the .pem file to a .ppk file: puttygen your_key_pair_name.pem -O private -o your_key_pair_name.ppk
. You will need to install putty for this task.
If you're using Git Bash, skip this step as it natively supports .pem files.
Open PuTTY and enter your instance's public IP address under "Host Name (or IP address)".
Navigate to "Connection" -> "SSH" -> "Auth" and browse for the .ppk file you created in the previous step.
Click "Open" to connect to the instance.
Install Required Software
Once connected to the instance, you will need to install the necessary software to run Laravel.
Update the package lists on your instance by running the following command: sudo apt-get update
Install PHP:
Laravel 10 requires PHP 8.1 or newer. In this demo, we will be using PHP 8.2, run the following command to install it along with some common extensions:
sudo apt install openssl php8.2 php8.2-fpm php8.2-cli php8.2-bcmath php8.2-curl php8.2-json php8.2-mbstring php8.2-pdo php8.2-mysql php8.2-tokenizer php8.2-xml php8.2-zip php8.2-gd
Install MySQL:
Most Laravel Application uses a database to store application data. To install MySQL, run the following command:
sudo apt-get install -y mysql-server
After installation, start the Nginx service by running: sudo systemctl start nginx
You can check if Nginx is running by visiting your instance's public IP address in your web browser.
Install Nginx:
Laravel requires a web server to serve the application. We will use Nginx. To install it, run the following command:
sudo apt-get install -y nginx
After installation, start the Nginx service by running: sudo systemctl start nginx
You can check if Nginx is running by visiting your instance's public IP address in your web browser.
Install Node.js:
Node.js is a JavaScript runtime that allows you to run JavaScript on the server-side. To install it, run the following commands:
curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash
sudo apt-get install -y nodejs
Install Git:
Git is a version control system that allows you to manage your codebase. To install it, run the following command:
sudo apt-get install -y git
Other useful software:
Some additional software that might be useful for your Laravel application:
Composer: A dependency manager for PHP. You can install it by running: sudo apt-get install -y composer
Redis: A fast in-memory key-value store. You can install it by running: sudo apt-get install -y redis-server
That's it! Your instance is now ready to host your Laravel application.
Clone your Laravel Application from Github
I would assume you store your application somewhere on Git Repository Provider such as GitHub or GitLab.
Change into the Laravel application directory:
mkdir /var/www/laravel
cd /var/www/laravel
Install the Laravel dependencies using Composer:
sudo composer install --no-dev
Configure Nginx to Serve Your Laravel Application
By default, Nginx is configured to serve static files from the /var/www/html directory. We will update the Nginx configuration file to serve our Laravel application instead.
Create a new server block for your Laravel application
First, create a new Nginx configuration file for your Laravel application:
sudo nano /etc/nginx/sites-available/laravel
Paste the following configuration into the file:
server {
listen 80;
server_name example.com; # Replace with your domain name
root /var/www/laravel/public;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.2-fpm.sock; # Replace with your PHP version
}
location ~ /\.ht {
deny all;
}
}
TODO :
- Replace example.com with your domain name.
- Replace
fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
with the appropriate PHP version and FPM socket path for your system.
Enable the server block
Next, create a symbolic link to enable the server block:
sudo ln -s /etc/nginx/sites-available/laravel /etc/nginx/sites-enabled/
Test the Nginx configuration to make sure there are no syntax errors:
sudo nginx -t
If the test is successful, restart the Nginx service to apply the changes:
sudo systemctl restart nginx
Update Laravel configuration
Laravel requires write access to the storage and bootstrap/cache
directories. Change the ownership and permissions on these directories:
sudo chown -R www-data:www-data /var/www/laravel/storage /var/www/laravel/bootstrap/cache
sudo chmod -R 775 /var/www/laravel/storage /var/www/laravel/bootstrap/cache
Finally, update the .env file in your Laravel project with your database credentials and other configuration options:
cd /var/www/laravel
cp .env.example .env
nano .env
Save the changes and generate a new application key:
php artisan key:generate
That's it! Your Laravel application should now be accessible at your server's public IP address or domain name.
Top comments (3)
Hello, I have this error
sudo apt install php8.2-cli
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package php8.2-cli
E: Couldn't find any package by glob 'php8.2-cli'
Oops didnt expect to have any reply after a year publish the article.
So the problem with that error is that php8.2 doesnt not exists in ubutnu 20.04 by default and you have to add ondrej/php repository in order for ubuntu to get to know where it is.
And it should work!
check out this ravitaxali.medium.com/how-to-deplo...