Ready to deploy the Laravel application
Deploy Laravel on AWS EC2: A Comprehensive Guide
Assuming that you have a ready-to-deploy Laravel application, we will use an Ubuntu platform for our server and walk through the process of setting up an EC2 instance and database.
New Laravel apps usually have some boilerplate code, which serves as a skeleton app from which to build. Because this lesson focuses on deployment, we will not modify this program. To have a quick peek, launch php artisan serve and visit http://localhost:8000 in your browser.
We’ll send our application to AWS as a zipped file, so go to your project directory and compress all of the files and folders within one zipped file. You do not need to add your vendor folder because it is likely rather large and unnecessary for setup.
Prerequisites
To complete this lesson, you will require the following:
- An Amazon Web Services account
- An EC2 instance running Amazon Linux 2 or any other suitable Linux distribution
- A fundamental grasp of the Linux command line.
- A Laravel application that is ready for deployment.
Let’s Get Started Now!
Step 1: Launch an EC2 instance.
The first step is to launch an EC2 instance on AWS. Navigate to the AWS Management Console and choose EC2 from the list of services. Click “Launch Instance” and select an appropriate AMI (Amazon Machine Image) for your needs.
Now it's time to connect to your created instance You can connect to the Instance using the SSH command provided or an SSH client like PuTTY / FileZilla to log in:
ssh -i your-key.pem ec2-user@your-public-ip
Or, if you are facing any trouble using these, don’t worry connect using EC2 Instance Connect with username “ubuntu”.
One more thing you want me to write a full guide on how to create and setup an EC2 instance, Please let me know by leaving a Comment.
Step 2: Prepare the Server
To check the Ubuntu version on the server use this command
lsb_release -a
2.1 Update the System
Run the following commands to update your packages:
sudo apt update && sudo apt upgrade -y # For Ubuntu
2.2 Install Required Software
Install necessary software like PHP, Composer, and a web server.
Install PHP and Extensions
sudo apt install php-cli php-fpm php-mysql php-xml php-mbstring php-curl unzip -y # Ubuntu
Install Composer
sudo curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
Install NGINX
sudo apt install nginx -y # Ubuntu
After Successfully Installing Ngnix on your server you can check it in etc directory using the command “cd etc” then “ls”
Now To check if nginx is running or not.
sudo systemctl status nginx
If everything we have done so far goes fine then you can see a welcome screen from ngnix when you will go on the public IP of your instance.
Step 3: Time to Deploy Laravel Application
3.1 Upload Your Laravel Code
- Use
scp
or an SFTP client like FileZilla to upload your Laravel application to the EC2 instance:
scp -i your-key.pem -r /path-to-your-laravel-project ec2-user@your-public-ip:/var/www/laravel-app
2. Move your app to /var/www/
:
sudo mv ~/laravel-app /var/www/laravel-app
You can also move your project here using Git for you need to install Git on your server using “sudo apt install git -y
”
Next, clone your Laravel project into the EC2 instance. Assuming you have Git installed on your EC2 instance, run the following command to clone your Laravel project:
git clone https://github.com/your_username/your_project.git /var/www/html
This will clone your Laravel project into the /var/www/html
directory.
scp (Secure Copy Protocol) is a command-line tool that allows you to securely transfer files between a local machine and a remote server or between two remote servers. It uses SSH (Secure Shell) to provide encryption and security during the transfer.
Key Features:
Security: Data is encrypted during transfer, ensuring privacy.
Ease of Use: Works directly from the command line.
Cross-Platform: Works on Linux, macOS, and Windows (with tools like PuTTY or OpenSSH).
3.2 Set Permissions (Be cautious at this step)
Set proper ownership and permissions:
sudo chown -R www-data:www-data /var/www/laravel-app
sudo chmod -R 775 /var/www/laravel-app/storage /
if working to already set instances don’t run these commands on the server without proper knowledge of how things going on your server, Because changing ownership may cause issues on the server.
3.3 Install Dependencies
Navigate to the app directory and install dependencies:
cd /var/www/laravel-app
composer install
3.4 Set Up the
.env
File
- Copy the example
.env
file:
cp .env.example .env
2. Edit the .env
file to configure your database and other settings:
nano .env
- Update
DB_HOST
,DB_PORT
,DB_DATABASE
,DB_USERNAME
, andDB_PASSWORD
.
3. Generate the application key:
php artisan key:generate
4: Configure NGINX
4.1 Create a Site Configuration
Create a new NGINX site configuration:
sudo nano /etc/nginx/sites-available/laravel
Add the following content:
server {
listen 80;
server_name your-domain-or-ip;
root /var/www/laravel-app/public;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
4.2 Enable the Site
- Link the configuration:
sudo ln -s /etc/nginx/sites-available/laravel /etc/nginx/sites-enabled/
2. Test the configuration:
sudo nginx -t
3. Restart NGINX:
sudo systemctl restart nginx
Till now we have done almost all the project-related setup and everything should be working fine including the server and dependencies, Let’s move to the Database part and get it all done.
5: Set Up the Database
- Install MySQL:
sudo apt install mysql-server -y # Ubuntu
2. Secure the installation:
sudo mysql_secure_installation
3. Create a database and user (and, To log in to SQL) :
You can do it using the below command:
CREATE DATABASE laravel_app; CREATE USER 'laravel_user'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON laravel_app.* TO 'laravel_user'@'localhost'; FLUSH PRIVILEGES;
sudo mysql -u root -p //to login into mysql
6: Finalize Laravel Configuration
6.1 Run Migrations
Run Laravel migrations to set up the database:
php artisan migrate
6.2 Set Up Caching
Optimize Laravel for production:
php artisan config:cache
php artisan route:cache
php artisan view:cache
You should be now able to see your app running on an instance public IP address also you can now map your DNS with this to move further,
if you are willing to install SSL on this domain you can also so it using the command given below
sudo apt install certbot python3-certbot-nginx -y
sudo certbot - nginx -d your-domain
Conclusion
Congratulations! You successfully launched your Laravel application to AWS EC2. While it may appear difficult at first, breaking the process down into small parts makes it accessible to everybody. Enjoy creating and growing your cloud apps!
I Hope this story adds some value in your new or existing Laravel application, I attempted to convey the basic notion. Thanks for reading this story if you find any mistake Please let me know.
If you find this Story Helpful, You can show some support to help me.