Laravel

Laravel:  Laravel is a free and open source PHP web framework for the development of web applications using MVC pattern (model view controller). It works with the help of Artisan CLI (command line interface).


Uses of Laravel fremwork:

  1. Clean and simple routing
  2. Best ORM and database layer
  3. Easy third-party integration
  4. Quicker and less complex
  5. Object-oriented libraries
  6. MVC pattern
  7. Database Migration



MVC benefits: 

  1. Separation of code (code separates in different parts)
  2. Fast application development
  3. Easy modification and debugging
  4. Large size view application can be developed easily

MVC pattern


Laravel installation:

1. Download composer

2. Open CMD and check composer is available or not by typing: composer

3. Install the laravel installer in the computer by typing: composer global require laravel/install

4. Laravel installation successful


Create a new project in Laravel: laravel new project_name 


Run project in the browser: php artisan serve

Server running on: [http://127.0.0.1:8000]

Change the port: php artisan serve --port=8080

Optimize the app: php artisan optimize


# Secure the laravel Project by Adding the .htaccess file:


<FilesMatch "^\.">

   Require all denied

</FilesMatch>


<Files .env>

   order allow,deny

   Deny from all

</Files>


<IfModule mod_rewrite.c>

   RewriteRule ^bootstrap/.* - [F,L]

</IfModule>


<IfModule mod_rewrite.c>

   RewriteEngine On

   # Deny access to all files in storage except public folder

   RewriteRule ^storage/(?!public/).* - [F,L]

</IfModule>


<IfModule mod_rewrite.c>

   RewriteRule ^vendor/.* - [F,L]

</IfModule>


<FilesMatch "composer\.(json|lock)">

   Require all denied

</FilesMatch>


<Files .git>

   order allow,deny

   Deny from all

</Files>


#run these command
# Set directory permissions to 775:
sudo find /opt/lampp/htdocs/ulos-new/storage -type d -exec chmod 775 {} \;
# Set file permissions to 664:
sudo find /opt/lampp/htdocs/ulos-new/storage -type f -exec chmod 664 {} \;


Summary of Permissions

  • 775 for directories:

    • Owner: Full access (read, write, execute).

    • Group: Full access (read, write, execute).

    • Others: Read and execute access (can list directory contents but cannot modify).

  • 664 for files:

    • Owner: Read and write access.

    • Group: Read and write access.

    • Others: Read access only.

Why These Permissions?

  • 775 for Directories: Allows users in the same group to navigate into the directory and access its contents, which is often necessary for applications like Laravel to function properly.

  • 664 for Files: Allows the owner and the group to modify the files, while others can only read them. This prevents unauthorized changes to the files while allowing necessary access.


# Change ownership of the storage directory:

sudo chown -R www-data:www-data /opt/lampp/htdocs/ulos-new/storage


# Change ownership of the bootstrap/cache directory (if needed):

sudo chown -R www-data:www-data /opt/lampp/htdocs/ulos-new/bootstrap/cache


Purpose: Ensures that the web server can read and write to the bootstrap/cache directory, which is important for caching configurations and optimizing performance.

Why Change Ownership?

  1. Web Server Access:

    • By changing the ownership of these directories to the web server user (www-data), you ensure that the web server has the necessary permissions to read from and write to these directories. This is crucial for the proper functioning of your Laravel application, especially for logging and caching operations.

  2. Prevent Permission Errors:

    • If the ownership is not set correctly, the web server may not have the right to access or modify the contents of these directories, leading to errors like:

      • Permission Denied: When the application tries to write logs or cache files.

      • File Not Found: When the application tries to read cached files or configuration.

  3. Security:

    • Proper ownership helps maintain security. By ensuring only the web server has access to these directories, you reduce the risk of unauthorized access or modifications from other users on the system.



Post a Comment

0 Comments

Visual Studio