avatarShaun Thornburgh

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

3421

Abstract

ess of managing and sharing PHP-based projects easier for us. Laravel utilises Composer to manage its dependencies so let’s get it installed in our server.</p> <figure id="bf93"> <div> <div>

            <iframe class="gist-iframe" src="/gist/shaunthornburgh/775d6d66174591e615db5310b7b69f29.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><figure id="81a9"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*Dk5AUWxdwiDhLhFNmH8vlA.png"><figcaption>Install Composer on AWS EC2</figcaption></figure><h2 id="9ec5">Step 6: Install MySQL &amp; Create Database</h2><p id="e012">MySQL is an open-source relational database management system (RDBMS) that allows users to store, organise, and retrieve data from structured sets of tables using SQL (Structured Query Language). 

Laravel is configured to use MySQL by default, to change its default database type, you can update <code>config/database.php</code>.</p><div id="5e97"><pre>sudo apt update sudo apt install mysql-server</pre></div><p id="8452">Once you have installed mysql, reset the root user password.</p><div id="1e6e"><pre>sudo mysql <span class="hljs-keyword">ALTER</span> <span class="hljs-keyword">USER</span> <span class="hljs-string">'root'</span>@<span class="hljs-string">'localhost'</span> IDENTIFIED <span class="hljs-keyword">WITH</span> mysql_native_password <span class="hljs-keyword">BY</span> <span class="hljs-string">'password'</span>; exit sudo systemctl restart nginx sudo systemctl restart php8<span class="hljs-number">.1</span><span class="hljs-operator">-</span>fpm</pre></div><figure id="79bf"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*Ic-mwyppSRoYrPXRxtXReg.png"><figcaption>Install Mysql on AWS EC2</figcaption></figure><h2 id="6c28">Step 7: Set Directory Permissions</h2><p id="27b9">The first step is to create a directory for our Laravel application.</p><p id="2a90">We need to change the permissions for so that it can be accessed without root privilages. We just need to change the owner to current logged-in user to have full access to this folder:</p><div id="2beb"><pre>sudo <span class="hljs-built_in">mkdir</span> -p /var/www/html/ sudo <span class="hljs-built_in">chown</span> -R ubuntu:ubuntu /var/www/html/</pre></div><figure id="0b4a"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*ygp3w_8JnW0h84CLjWsvFg.png"><figcaption>Set directory permissions</figcaption></figure><h2 id="0101">Step 8: Install Laravel</h2><p id="c999">At this stage you can either install a fresh Laravel application or clone an existing repository. In this article we will install Laravel directory on to this server, if you would like to know more about automated deplyments I have written another article on this:</p><div id="b1b3" class="link-block"> <a href="https://readmedium.com/how-to-build-test-and-deploy-laravel-app-126d6ac7404f"> <div> <div> <h2>How to Build, Test, and Deploy Laravel App</h2> <div><h3>A Beginners guide to deployments using Amazon EC2 and GitHub Actions</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.c

Options

om/v2/resize:fit:320/1*IjJRrcaRGG2C3hrZDGLghg.png)"></div> </div> </div> </a> </div><p id="a457">Run the following to install Laravel directly onto the server:</p><div id="a19a"><pre><span class="hljs-built_in">cd</span> /var/www/html/ composer create-project - prefer-dist laravel/laravel laravel-install-test</pre></div><figure id="ccef"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*JWTEVMsZnHRY1vUpjatjrQ.png"><figcaption>Install Laravel using Composer</figcaption></figure><h2 id="5383">Step 9: Create Folders</h2><p id="447c">We need to create the following folders and update permissions.</p><div id="bbcf"><pre><span class="hljs-built_in">cd</span> laravel-install-test/ <span class="hljs-built_in">chmod</span> -R ugo+rw storage/logs <span class="hljs-built_in">mkdir</span> -p framework/{sessions,views,cache} <span class="hljs-built_in">chmod</span> -R ugo+rw storage/framework</pre></div><figure id="2e68"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*FqRV68IqN9HQx4XOQzazOQ.png"><figcaption>Create folders</figcaption></figure><h2 id="addc">Step 10: Update Default Nginx Config</h2><p id="71c7">As we are only installing one site on this machine we can just update <code>/etc/nginx/sites-enabled/default</code>.</p><div id="15a8"><pre>server { listen 80 default_server; listen [::]:80 default_server;

   root /var/www/html/laravel-install-test/public/;

   index index.php index.html index.htm index.nginx-debian.html;

   server_name _;

   location / {
           try_files <span class="hljs-variable">$uri</span> <span class="hljs-variable">$uri</span>/ /index.php?<span class="hljs-variable">$query_string</span>;
   }

   location ~ \.php$ {
           include snippets/fastcgi-php.conf;
           fastcgi_pass unix:/run/php/php8.1-fpm.sock;
   }

}</pre></div><figure id="b8ec"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*C0svYeytRZ3hD1vmMXavOA.png"><figcaption>Update Default Nginx Config</figcaption></figure><p id="044e">Save the file and restart Nginx.</p><div id="d576"><pre>sudo systemctl restart nginx</pre></div><h2 id="d0f9">Step 11: Validate Installation</h2><p id="cdb4">You can now validate your installation by visiting the public IP address for your EC2 instance.</p><figure id="69bf"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*eSiAkSM3sCdvQUrq-omu0Q.png"><figcaption>Laravel Installtion Successful on AWS EC2</figcaption></figure><p id="149f">Congratulations! You now have a Laravel installation on a live AWS EC2 instance :)</p><h1 id="6588">Conclusion</h1><p id="7785">In this article, we have walked through the process of setting up Laravel on an EC2 instance, breaking down each step which I hope you found easy to understand.</p><p id="dffa">Amazon’s EC2 offers scalable and robust infrastructure, and combining it with Laravel presents a powerful toolset for web development. Please bear in my mind this is a very basic installation and it’s essential to learn more about security and maintenance aspects, regularly updating both your server and Laravel application to keep up with the latest patches and updates.</p><p id="0ae5">As always, continual learning and adapting to the ever-evolving tech landscape will ensure optimal performance and security for your applications.</p></article></body>

Provisioning an AWS EC2 Instance for a Laravel Application

AWS EC2, offers scalable cloud computing capabilities, making it an ideal environment for hosting Laravel applications. In this article, we will walk through the steps of installing Laravel on an AWS EC2 server.

How to Provision an AWS EC2 Instance for a Laravel Application

Prerequisites:

  • An AWS account

Step 1: Create an EC2 Instance

  1. Log into your AWS Management Console.
  2. Access the EC2 dashboard, and click “Launch Instance”.
  3. Enter a suitable name for your instance.
  4. Select Ubuntu AMI (Amazon Machine Image).
  5. Choose your instance type, “t2.micro” is available on the free tier.
  6. If you don’t already have a key pair, create one.
  7. Select network settings, you will need to allow SSH and HTTP connections. The standard storage configuration will be suitable for our requirements.
  8. Review your settings, and click “Launch Instance”.

Step 2: Log in to your EC2 Instance

Use the key you created in the previous step you can now connect to your server via SSH.

ssh -i ~/.ssh/EC2.pem ubuntu@[YOUR SERVER IP]
Connect to AWS EC2 via SSH

Step 3: Install NGINX

Nginx (pronounced “engine-x”) is a high-performance web server, reverse proxy, load balancer, and HTTP cache. It is free and open-source software and is a popular web server choice due to its speed, stability, and scalability. Run the following commands to install:

sudo apt update
sudo apt install nginx
systemctl status nginx
sudo systemctl restart nginx
sudo nginx -t
Install Nginx on AWS EC2

Step 4: Install PHP

Laravel is built using PHP so we are going to have to install this!

sudo apt update
sudo apt install - no-install-recommends php8.1
sudo apt-get install php8.1-cli php8.1-common php8.1-mysql php8.1-zip php8.1-gd php8.1-mbstring php8.1-curl php8.1-xml php8.1-bcmath php8.1-fpm
Install PHP on AWS EC2

Step 5: Install Composer

Composer is a dependency management tool for PHP, allowing developers to declare the libraries on which their projects depend and automatically manage their inclusion and versioning. By specifying a list of required packages in a composer.json file, you can leverage Composer to download and update the necessary libraries and their respective dependencies, ensuring that the project has the correct versions of each library. This makes the process of managing and sharing PHP-based projects easier for us. Laravel utilises Composer to manage its dependencies so let’s get it installed in our server.

Install Composer on AWS EC2

Step 6: Install MySQL & Create Database

MySQL is an open-source relational database management system (RDBMS) that allows users to store, organise, and retrieve data from structured sets of tables using SQL (Structured Query Language). Laravel is configured to use MySQL by default, to change its default database type, you can update config/database.php.

sudo apt update
sudo apt install mysql-server

Once you have installed mysql, reset the root user password.

sudo mysql
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
exit
sudo systemctl restart nginx
sudo systemctl restart php8.1-fpm
Install Mysql on AWS EC2

Step 7: Set Directory Permissions

The first step is to create a directory for our Laravel application.

We need to change the permissions for so that it can be accessed without root privilages. We just need to change the owner to current logged-in user to have full access to this folder:

sudo mkdir -p /var/www/html/
sudo chown -R ubuntu:ubuntu /var/www/html/
Set directory permissions

Step 8: Install Laravel

At this stage you can either install a fresh Laravel application or clone an existing repository. In this article we will install Laravel directory on to this server, if you would like to know more about automated deplyments I have written another article on this:

Run the following to install Laravel directly onto the server:

cd /var/www/html/
composer create-project - prefer-dist laravel/laravel laravel-install-test
Install Laravel using Composer

Step 9: Create Folders

We need to create the following folders and update permissions.

cd laravel-install-test/
chmod -R ugo+rw storage/logs
mkdir -p framework/{sessions,views,cache}
chmod -R ugo+rw storage/framework
Create folders

Step 10: Update Default Nginx Config

As we are only installing one site on this machine we can just update /etc/nginx/sites-enabled/default.

server {
       listen 80 default_server;
       listen [::]:80 default_server;

       root /var/www/html/laravel-install-test/public/;

       index index.php index.html index.htm index.nginx-debian.html;

       server_name _;

       location / {
               try_files $uri $uri/ /index.php?$query_string;
       }

       location ~ \.php$ {
               include snippets/fastcgi-php.conf;
               fastcgi_pass unix:/run/php/php8.1-fpm.sock;
       }
}
Update Default Nginx Config

Save the file and restart Nginx.

sudo systemctl restart nginx

Step 11: Validate Installation

You can now validate your installation by visiting the public IP address for your EC2 instance.

Laravel Installtion Successful on AWS EC2

Congratulations! You now have a Laravel installation on a live AWS EC2 instance :)

Conclusion

In this article, we have walked through the process of setting up Laravel on an EC2 instance, breaking down each step which I hope you found easy to understand.

Amazon’s EC2 offers scalable and robust infrastructure, and combining it with Laravel presents a powerful toolset for web development. Please bear in my mind this is a very basic installation and it’s essential to learn more about security and maintenance aspects, regularly updating both your server and Laravel application to keep up with the latest patches and updates.

As always, continual learning and adapting to the ever-evolving tech landscape will ensure optimal performance and security for your applications.

Laravel
PHP
AWS
DevOps
Recommended from ReadMedium