MalcMind

PHP - Getting Started

php.png
Todo: You may also want to install common PHP extensions. For example, to install the MySQL extension:

  • how to install php
1. `sudo apt install php`
2. `php --version`

Differences Between PHP and NODE

Yes, that's correct. Node.js can run without Apache or Nginx. Unlike traditional server-side languages like PHP, Node.js has its built-in server capabilities. You can create a simple HTTP server directly using Node.js without the need for a separate web server like Apache or Nginx.

What is Composer

Similar to Node's npm Composer is a dependency manager for PHP, and it's widely used in the PHP community, including with Laravel. It simplifies the process of managing and installing external PHP libraries and packages required for a project. With Composer, you can declare the libraries your project depends on and manage them efficiently.

To use Composer in a Laravel project, you typically find a composer.json file in the root of the project directory. This file contains the project's dependencies, and running composer install installs those dependencies.

Laravel itself is managed as a Composer package, and when you create a new Laravel project using composer create-project laravel/laravel, Composer installs the Laravel framework and its dependencies.

How to Install Composer

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" php composer-setup.php sudo mv composer.phar /usr/local/bin/composer

To verify the installation, you can run:

bashCopy code

composer --version

This should display the installed Composer version.

  1. php: This part of the command is calling the PHP interpreter.

  2. -r: This option allows you to run PHP code from the command line. The following string is the PHP code that will be executed.

  3. The PHP code itself:

    phpCopy code

    copy('https://getcomposer.org/installer', 'composer-setup.php');

    • copy('https://getcomposer.org/installer', 'composer-setup.php');: This PHP code uses the copy function to download the Composer installer from https://getcomposer.org/installer and save it as composer-setup.php in the current directory.

what does this do sudo mv composer.phar /usr/local/bin/composer

  1. sudo: This command is used to execute the following command with superuser (administrative) privileges. It stands for "superuser do."

  2. mv: This command is used to move or rename files and directories.

  3. composer.phar: This is the source file that is being moved. It's the Composer executable file that you downloaded and named composer.phar during the installation.

  4. /usr/local/bin/composer: This is the destination path where the composer.phar file is being moved. Placing the Composer executable in the /usr/local/bin/ directory allows you to run the composer command from any location in the terminal without specifying the full path.

So, the command is effectively moving the Composer executable to a directory in the system's PATH (/usr/local/bin/), making it globally accessible, and renaming it to just composer for convenience. After this command, you should be able to run composer from any directory in the terminal.

Laravel

Laravel does not strictly require Apache as its web server. Laravel can run on various web servers, and one of the most commonly used options is Nginx. Laravel also has its built-in development server that you can use for testing and local development.

How to create a Laravel Project

In the folder of your choise run

create-project laravel/laravel example-app

Common errors

Problem 1 - laravel/pint[v1.0.0, ..., v1.13.6] require ext-xml * -> it is missing from your system. Install or enable PHP's xml extension. - Root composer.json requires laravel/pint ^1.0 -> satisfiable by laravel/pint[v1.0.0, ..., v1.13.6]. To enable extensions, verify that they are enabled in your .ini files: - /etc/php/8.1/cli/php.ini

The error you're encountering indicates that the laravel/pint package has a dependency on the ext-xml PHP extension, which is missing or not enabled on your system. The ext-xml extension is essential for handling XML-related functionality in PHP.

To resolve this issue, you need to install or enable the ext-xml extension. The steps to do this may vary depending on your operating system. Below are general instructions for common systems:

For Ubuntu (or Debian-based systems):

sudo apt-get install php-xml

Make sure to restart your PHP service or web server after installing the extension.

If you're using PHP on the command line (CLI), you might need to edit the appropriate php.ini file to ensure the ext-xml extension is enabled. The location of the php.ini file can be found by running:

php --ini

Open the indicated php.ini file and make sure the following line is uncommented (remove the semicolon ; at the beginning if present):

extension=xml

After making these changes, try running the Composer command again:

composer create-project laravel/laravel example-app

error 2

Your requirements could not be resolved to an installable set of packages. Problem 1 - spatie/laravel-ignition[2.0.0, ..., 2.3.1] require ext-curl * -> it is missing from your system. Install or enable PHP's curl extension. - Root composer.json requires spatie/laravel-ignition ^2.0 -> satisfiable by spatie/laravel-ignition[2.0.0, ..., 2.3.1]. To enable extensions, verify that they are enabled in your .ini files: - /etc/php/8.1/cli/php.ini

The error is indicating that the spatie/laravel-ignition package requires the ext-curl PHP extension, and it's missing or not enabled on your system.

To resolve this issue, you can follow these steps to install or enable the curl extension:

For Ubuntu (or Debian-based systems):

sudo apt-get install php-curl

If you're using PHP on the command line (CLI), you might need to edit the appropriate php.ini file to ensure the ext-curl extension is enabled. The location of the php.ini file can be found by running:

php --ini

Open the indicated php.ini file and make sure the following line is uncommented (remove the semicolon ; at the beginning if present):

extension=curl

Built-in Development Server:

Laravel comes with a built-in development server that you can use during the development phase. To use it, navigate to your Laravel project directory in the terminal and run:

php artisan serve

This will start a development server, and you can access your Laravel application at http://localhost:8000 in your web browser.

What is artisan

Artisan is the name of the command-line interface (CLI) included with Laravel, a popular PHP web application framework. Artisan provides a number of helpful commands for various common tasks when developing a Laravel application. It helps developers perform tasks such as database migrations, seeding databases with sample data, generating boilerplate code for controllers and models, and more.

How to install Artisan?

Artisan is not something you install separately; it comes bundled with the Laravel framework. Laravel is a PHP web application framework, and Artisan is the command-line interface (CLI) included with Laravel.

To use Artisan, you typically open a terminal or command prompt, navigate to your Laravel project directory, and run commands using the php artisan syntax.

Here are a few examples of what Artisan can do:

Here are a few examples of what Artisan can do:

  1. Database Migrations:

    bashCopy code

    php artisan migrate

    Runs any outstanding database migrations.

  2. Database Seeding:

    bashCopy code

    php artisan db:seed

    Seeds the database with sample data.

  3. Creating a Controller:

    bashCopy code

    php artisan make:controller MyController

    Generates a new controller file with boilerplate code.

  4. Creating a Model:

    bashCopy code

    php artisan make:model MyModel

    Creates a new model file.

  5. Serving the Application:

    bashCopy code

    php artisan serve

    Starts the built-in development server.

  6. List All Available Artisan Commands:

    bashCopy code

    php artisan list

    Displays a list of all available Artisan commands.

Artisan is a powerful tool that simplifies many common development tasks, making Laravel development more efficient. Laravel developers often use Artisan commands throughout the development process to streamline tasks and adhere to Laravel's conventions. The ability to extend Artisan with custom commands also allows developers to tailor it to their specific project requirements.

Starting the Apach2 server

`sudo service apache2 start`

Stoping Apache2 server

sudo service apache2 stop

Also note: 1. Nginx is a popular web server that is often used to serve Laravel applications in production. You can configure Nginx to work with Laravel by creating an Nginx server block (similar to Apache's virtual host). Laravel provides example Nginx configurations in the documentation.

Running a Sample PHP program

To save the PHP code to an index.php file on Ubuntu, you can use a text editor like Nano or Vim. Here are examples for both editors:

Using Nano

  1. Open a terminal.

  2. Type the following command to open Nano:

    bashCopy code

    nano index.php

  3. Paste your PHP code into the editor.

  4. Save the file by pressing Ctrl + O, then press Enter.

  5. Exit Nano by pressing Ctrl + X.

Making the PHP File

You can create a new file with a .php extension (e.g., index.php) and then add the following code:

<?php
// Simple PHP script

// Displaying a message
echo "Hello, World!";

// Variables
$name = "John";
$age = 25;

// Displaying variables
echo "<br>";
echo "Name: " . $name . "<br>";
echo "Age: " . $age;

// Conditional statement
if ($age >= 18) {
    echo "<br>You are an adult.";
} else {
    echo "<br>You are a minor.";
}
?>

If you are using a local development server on macOS or Linux, you can save the file in the appropriate web server directory (e.g., /var/www/html/ for Apache on Ubuntu) and access it similarly.

MISC

  • PHPStorm is an IDE
  • Laravel Pint - Code quality tool
  • Routing In php/Laravel - https://laravel.com/docs/10.x/routing
  • Inertia library helps it integrate with front end framewokrs
  • https://inertiajs.com/
  • https://laravel.com/docs/10.x/frontend - serverside rendering
  • Larvel uses - MVC
  • starter kits - https://laravel.com/docs/10.x/starter-kits#breeze-and-inertia (tailwind css, react, nextjs)
  • Blade -templating engine - https://laravel.com/docs/10.x/blade
  • Livewire - alternative to react and compliments blade https://livewire.laravel.com/docs/quickstart
  • Socialite - user auth Laravel sanctum
  • cashier - payments
  • By default confuigured with mysql
  • Eloquent - orm makes code readble
  • Good video resourse - https://www.google.com/search?sca_esv=582587607&rlz=1C1ONGR_enUS1027US1027&sxsrf=AM9HkKnK_0azzzT4SDpD9CYebb3cGswFyQ:1700045859415&q=php+laravel&tbm=vid&source=lnms&sa=X&sqi=2&ved=2ahUKEwiVlLWs7MWCAxU2D1kFHWZ0DHAQ0pQJegQICxAB&biw=1277&bih=824&dpr=1.1#fpstate=ive&vld=cid:c75e5df3,vid:rIfdg_Ot-LI,st:0

NGINX

nginx configuration

ChatGPT

Configuring Nginx involves editing its configuration files to define how it should handle web requests. The main configuration file is usually named nginx.conf, but Nginx commonly uses separate files for server blocks (similar to virtual hosts in Apache), making the configuration modular and easier to manage.

Here's a basic overview of Nginx configuration:

  1. Main Configuration File (nginx.conf):

    • Location: /etc/nginx/nginx.conf or /usr/local/nginx/conf/nginx.conf (location may vary based on your installation method and operating system).
    • This file contains global settings that apply to the entire Nginx server. You might not need to modify this file frequently.
  2. Server Block Configuration Files:

    • Location: Typically in /etc/nginx/sites-available/ or /etc/nginx/conf.d/.

    • Server block configuration files define how Nginx should handle requests for specific domains or IP addresses.

    • Example server block configuration file (/etc/nginx/sites-available/example.com):

      nginxCopy code

      server { listen 80; server_name example.com www.example.com; root /var/www/example.com/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/php7.4-fpm.sock; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } location ~ /\.ht { deny all; } error_log /var/log/nginx/example.com_error.log; access_log /var/log/nginx/example.com_access.log; }

  3. Symlink to Enable a Site:

    • On systems using sites-available and sites-enabled, you create a symbolic link to enable a site:

      bashCopy code

      sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/

  4. Test Configuration and Reload Nginx:

    • Before restarting Nginx, test the configuration to catch syntax errors:

      bashCopy code

      sudo nginx -t

    • If the test is successful, reload Nginx to apply the changes:

      bashCopy code

      sudo systemctl reload nginx

This is a basic example, and the configuration can get more complex based on your needs. Ensure you have a backup of your configuration files before making changes. Additionally, refer to the official Nginx documentation for more detailed information on configuration directives and best practices.