PHP
Install
# On Ubuntu 20.04, the command installs PHP 7.4 while Ubuntu 22.04 installs PHP version 8.1.
sudo apt install php
# Ensure you are using the latest repository updates by entering the following command
sudo apt update && sudo apt upgrade
# Install software-properties-common to help you manage distributions and independent software sources
sudo apt install software-properties-common
# add the ondrej/php PPA which provides different PHP versions for Ubuntu
sudo add-apt-repository ppa:ondrej/php
# After the PPA loads, press Enter to confirm adding the repository. The available PHP versions in the PPA are from 5.6 up to 8.2
sudo apt update
# to install PHP 7.4, run the command
sudo apt -y install php7.4
# To install PHP 8.1, run the following command
sudo apt -y install php8.1
# Verify the installation
php -v
# To install PHP for Nginx
sudo apt install php8.1-fpm -y
# Once the installation finishes, restart the Nginx service to apply the changes
sudo systemctl restart nginx
# enable PHP support by editing the server block
sudo vim /etc/nginx/sites-available/default
## edit the configuration file
sudo systemctl restart nginx
sudo systemctl reload php<version_number>-fpm
# To install additional PHP modules
sudo apt install php<version>-<package_name>
# sudo apt install php8.1-posix
# install multiple modules at once
sudo apt install php7.4-{mysql,zip,bcmath}
# To list all loaded PHP modules
php -m
server {
# . . . existing configuration
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php<version>-fpm.sock;
}
}
Uninstall
sudo apt-get purge php<version>
sudo apt-get autoremove
Composer install
Composer is not a package manager in the same sense as Yum or Apt are. Yes, it deals with “packages” or libraries, but it manages them on a per-project basis, installing them in a directory (e.g. vendor) inside your project. By default, it does not install anything globally. Thus, it is a dependency manager. It does however support a “global” project for convenience via the global command. |
# At project root, local install
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === 'e21205b207c3ff031906575712edab6f13eb0b361f2085f1f1237b7126d785e826a450292b6cfd1d64d92e6563bbde02') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"
## Global install
sudo mv composer.phar /usr/local/bin/composer
## create new laravel/laravel project
composer create-project laravel/laravel example-app
cd example-app
php artisan serve
# Once you have started the Artisan development server, your application will be accessible in your web browser at http://localhost:8000
Dockerfile
FROM php:7.4-fpm
# Arguments defined in docker-compose.yml
ARG user
ARG uid
# Install system dependencies
RUN apt-get update && apt-get install -y \
git \
curl \
libpng-dev \
libonig-dev \
libxml2-dev \
zip \
unzip
# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
# Install PHP extensions
RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd fileinfo intl
# Get latest Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
# Create system user to run Composer and Artisan Commands
RUN useradd -G www-data,root -u $uid -d /home/$user $user
RUN mkdir -p /home/$user/.composer && \
chown -R $user:$user /home/$user
# Set working directory
WORKDIR /var/www
USER $user
docker-compose.yml
version: "3.7"
services:
app:
build:
args:
user: sammy
uid: 1000
context: ./mt_api
dockerfile: Dockerfile
image: my_php_image
container_name: app
restart: always
working_dir: /var/www/
volumes:
- ./app_root:/var/www
networks:
- travellist
networks:
travellist:
driver: bridge
build
docker-compose build app
docker-compose up -d
docker-compose ps
docker-compose exec app ls -l
# ...app_root
docker-compose exec app rm -rf vendor composer.lock
docker-compose exec app composer install
# Package manifest generated successfully.
docker-compose exec app php artisan key:generate
# Application key set successfully.
# http://server_domain_or_IP:8000
laravel nginx
server {
listen 80;
listen [::]:80;
server_name example.com;
root /srv/example.com/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
index index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
Composer commands
command | description |
---|---|
about | Shows a short information about Composer |
archive | Creates an archive of this composer package |
audit | Checks for security vulnerability advisories for installed packages |
browse | [home] Opens the package’s repository URL or homepage in your browser |
bump | Increases the lower limit of your composer.json requirements to the currently installed versions |
check-platform-reqs | Check that platform requirements are satisfied |
clear-cache | [clearcache | cc] Clears composer’s internal package cache |
completion | Dump the shell completion script |
config | Sets config options |
create-project | Creates new project from a package into given directory |
depends | [why] Shows which packages cause the given package to be installed |
diagnose | Diagnoses the system to identify common errors |
dump-autoload | [dumpautoload] Dumps the autoloader |
exec | Executes a vendored binary/script |
fund | Discover how to help fund the maintenance of your dependencies |
global | Allows running commands in the global composer dir ($COMPOSER_HOME) |
help | Display help for a command |
init | Creates a basic composer.json file in current directory |
install | [i] Installs the project dependencies from the composer.lock file if present, or falls back on the composer.json |
licenses | Shows information about licenses of dependencies |
list | List commands |
outdated | Shows a list of installed packages that have updates available, including their latest version |
prohibits | [why-not] Shows which packages prevent the given package from being installed |
reinstall | Uninstalls and reinstalls the given package names |
release-script | Runs the release-script script as defined in composer.json |
remove | Removes a package from the require or require-dev |
require | [r] Adds required packages to your composer.json and installs them |
run-script | [run] Runs the scripts defined in composer.json |
search | Searches for packages |
self-update | [selfupdate] Updates composer.phar to the latest version |
show | [info] Shows information about packages |
status | Shows a list of locally modified packages |
suggests | Shows package suggestions |
update | [u|upgrade] Updates your dependencies to the latest version according to composer.json, and updates the composer.lock file |
validate | Validates a composer.json and composer.lock |
artisan commands
- ` php artisan list`