How to Install Laravel 10.10 with php 8.1 ,Apache2 and Conneted to MySQL on AWS Ubuntu 22.04 LTS

How to Install Laravel 10.10 with php 8.1 ,Apache2 and Conneted to MySQL on AWS Ubuntu 22.04 LTS

Table of contents

No heading

No headings in the article.

I will assume that the ubuntu instance has been set up with a public IP and associated security groups to enable port 80 (HTTP) and 22 (SSH)

Connect to your ubuntu instance.

1. Install Apache:-

sudo apt update
sudo apt install apache2

After that, we can check the Apache service status .

sudo systemctl status apache2

Open a web browser and we go to the IP address of your server or its domain name.If you see this screen, that means Apache is up and running.

2. Install PHP 8.1

sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt install php8.1 libapache2-mod-php8.1
sudo systemctl restart apache2

3. Install required PHP extensions for Laravel 10.10

sudo apt install php8.1-common php8.1-bcmath php8.1-curl php8.1-dom php8.1-mbstring php8.1-zip

4. Install Composer

curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
sudo chmod +x /usr/local/bin/composer

5. Install Laravel 10.10 using composer

composer create-project --prefer-dist laravel/laravel sammy-php
cd sammy-php
php artisan

6. Installing MySQL

sudo apt install mysql-server
sudo mysql_secure_installation

When you’re finished, test if you’re able to log in to the MySQL console

sudo mysql

This will connect to the MySQL server as the administrative database user root

To exit the MySQL console,write the following:

exit

7. Creating a Database for the Application

sudo mysql

To create a new database, run the following command from your MySQL console:

CREATE DATABASE student;

Now, you can create a new user and grant them full privileges on the custom database you’ve just created.

CREATE USER 'sammy_user'@'%' IDENTIFIED WITH mysql_native_password BY 'write-your-password';

Now we need to give this user permission over the student database:

GRANT ALL ON student.* TO 'sammy_user'@'%';
exit

You can now test if the new user has the proper permissions by logging in to the MySQL console

mysql -u sammy_user -p

Note the -p flag in this command, which will prompt you for the password used when creating the sammy_user user.

SHOW DATABASES;

Next, create a table named studentname in the student database. From the MySQL console, run the following statement:

mysql> CREATE TABLE student.studentname (
    -> id INT AUTO_INCREMENT,
    -> name VARCHAR(255),
    -> attendance BOOLEAN,
    -> PRIMARY KEY(id)
    -> );

Now, populate the attendance table with some sample data:

mysql> INSERT INTO student.studentname (name, attendance) 
    -> VALUES ("Ruchi", true),
    -> ("Rohita", true),
    -> ("Krisha", false),
    -> ("Safal", true),
    -> ("Rajesh", false);

To confirm that the data was successfully saved to your table, run:

mysql> SELECT * FROM student.studentname;

After confirming that you have valid data in your test table, you can exit the MySQL console:

mysql> exit

8. Configuring Larvel

By default, these values are configured for a local development environment that uses Homestead, a prepackaged Vagrant box provided by Laravel. We’ll change these values to reflect the current environment settings of our application.

cd sammy-php
vi .env
APP_NAME=StudentName
APP_ENV=development
APP_KEY=base64:aIstLda7/HejviRDOYGpEbSLr0bF8BiYBFEvRty/I4M=
APP_DEBUG=true
APP_URL=http://your domain_or_IP

LOG_CHANNEL=stack

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=student
DB_USERNAME=sammy_user
DB_PASSWORD=write-your-password

Generate a new APP_KEY

php artisan key:generate
sudo apt install php8.1-pdo php8.1-mysql
php artisan migrate

9. Setting Up Apache2

We need to move the previously created project directory to the Apache web root.

sudo mv sammy-php /var/www/html/

After that, set the necessary permissions to ensure the project runs smoothly:

sudo chgrp -R www-data /var/www/html/sammy-php/
sudo chmod -R 775 /var/www/html/sammy-php/storage

It is necessary to create a new virtual host for the project. It can be done easily with the commands below:

cd /etc/apache2/sites-available
vi sammy_project.conf

Copy this content to your sammy_project.conf

<VirtualHost *:80>
   ServerName 3.135.235.87 
   ServerAdmin webmaster@3.135.235.87 
   DocumentRoot /var/www/html/sammy-php/public

   <Directory /var/www/html/sammy-php>
       AllowOverride All
   </Directory>
   ErrorLog ${APACHE_LOG_DIR}/error.log
   CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Save the file and close it.

After that, disable the default configuration file of the virtual hosts in Apache with this command:

sudo a2dissite 000-default.conf

Afterwards, enable the new virtual host:

sudo a2ensite sammy_project

Enable the Apache rewrite module, and finally, restart the Apache service:

sudo a2enmod rewrite
sudo systemctl restart apache2

Now go to your browser and access the application using the server’s domain name or IP address, as defined by the server_name directive in your configuration file:

You will see a page like this:

9. Customizing the main page

We’ll now edit the main application route to query for the database and return the contents to the application’s view.

Open the main route file, routes/web.php:

cd /var/www/html/sammy-php/routes
vi web.php

Copy this content to your routes/web.php file, replacing the code that is already there:

<?php

use Illuminate\Support\Facades\DB;

Route::get('/', function () {
    // Select students who attended (assuming there's a column named 'attendance' in the table)
    $attendance = DB::table('studentname')->where('attendance', 1)->get();

    // Select students who did not attend
    $togo = DB::table('studentname')->where('attendance', 0)->get();

    return view('student', ['attendance' => $attendance, 'togo' => $togo]);
});

Save and close the file when you’re done editing.

We’ll now create the view that will render the database results to the user.

cd /var/www/html/sammy-php/resources/views
# replace welcome.blade.php to student.blade.php
vi student.blade.php

paste the given below content

<html>
<head>
        <title>Student Name</title>
</head>

<body>
        <h1>Student Attendance List</h1>
        <h2>Student who are absent</h2>
        <ul>
          @foreach ($togo as $absent)
                <li>{{ $absent->name }}</li>
          @endforeach
        </ul>

        <h2>Student who are present</h2>
        <ul>
          @foreach ($attendance as $present)
                <li>{{ $present->name }}</li>
          @endforeach
        </ul>
</body>
</html>

Save and close the file when you’re done. Now go to your browser and reload the application.

if you got an error like this then again give the permission

sudo chgrp -R www-data /var/www/html/sammy-php/
sudo chmod -R 775 /var/www/html/sammy-php/storage
sudo systemctl restart apache2

You’ll see a page like this:

You have now a functional Laravel application pulling contents from a MySQL database.