ApacheWebServer PHP
ApacheWebServer PHP
1-Introduction
2-Client, Server, and URLs
3-Requests, methods, and responses
4-Apache core, modules, and process model
5-Install apache, configuration, and logs
6-Apache main server configurations
2
Apache Web Server Index
8-Apache authentication
HTTP is Plain text protocol uses port tcp/80, HTTPS uses tcp/443 for ssl connections.
4
Apache Web Server 2-Client, Server, URLS
Client sends request to a resource to the web server socket using server URL.
The web server receives the request, and extracts the Request URI.
The web server reads in the resource from the URI and gets it from the local file system.
Sends back the response to the client associated with response code.
5
Apache Web Server 2-Client, Server, URLS
URL is a string of characters to identify a resource.
Example 1: http://192.168.1.2/b.php?fname=ahmed&fage=45
Protocol : http
HTTP Host: 192.168.1.2:80 – The port is removed because it is the default port-
URL-Path : b.php
Query String: ?fname=ahmed&fage=45.
/var/www/html called DirectoryRoot
b.php will be relative to the DirectoryRoot
URL-Path/QueryString called Request URI
Example 2: http://192.168.1.3:8080/data/b.php?fname=ahmed&fage=45
Protocol : http
HTTP Host: 192.168.1.2:8080
URL-Path : data/b.php
Query String: ?fname=ahmed&fage=45.
b.php will be under directory data under the DirectoryRoot
URL-Path/QueryString called Request URI
6
Apache Web Server 3-Requests, methods,
and responses
Request:
Client sends the request to access a resource hosted on the web server.
The HTTP request is part of the network packet, and resides in the packet data payload.
7
Apache Web Server 3-Requests, methods,
and responses
The HTTP response is part of the network packet, and resides in the packet data payload.
9
Apache Web Server 3-Requests, methods,
and responses
10
HTTP Response packet format captured by wireshark
Apache Web Server 3-Requests, methods,
and responses
Methods:
How the data transfer takes place between the client and the server.
11
Apache Web Server 3-Requests, methods,
and responses
GET Method:
Can be cached
Can be bookmarked
12
Apache Web Server 3-Requests, methods,
and responses
GET Method:
14
Apache Web Server 3-Requests, methods,
and responses
POST Method:
If no error, the requested object returned in the HTTP Response message body.
Examples:
200 : OK
401: Unauthorized
403: Forbidden
404: Not found
500: Internal Server Error 16
Apache Web Server 3-Requests, methods,
and responses
Responses:
17
HTTP Response from wireshark with OK status
Apache Web Server 3-Requests, methods,
and responses
Responses:
18
HTTP Response from wireshark with 404 status
Apache Web Server 4-Apache core, modules,
and process model
Apache is a free and open source package on Linux/unix platforms.
Configuration file is divided into smaller files and loaded by Include directive.
19
Apache Web Server 4-Apache core, modules,
and process model
Apache is modular by design.
Modularity allows the administrator to choose which features needed to enable it.
Also, allows administrator to disable the modules with not needed features.
Modules either:
Base : Loadad at compile time – Static modules –
Extension : Loaded at run-time – Dynamic modules –
20
Apache Web Server 4-Apache core, modules,
and process model
Process model.
Master process starts and listen on default port tcp/80 (why must start with root),
from file “/etc/apache2/ports.conf”
Prefork module.
Worker module.
Event module. 21
Apache Web Server 4-Apache core, modules,
and process model
Prefork MPM:
22
Apache Web Server 4-Apache core, modules,
and process model
Worker MPM:
The master process listens for incoming connection to the socket, and pass
the connection to 1st available thread.
Master process forks multiple processes, and each process launches multi-threads.
Event MPM:
The same as Worker MPM, but designed to allow more requests to be served.
23
Apache Web Server 5-Install apache,
configuration, and logs
Install
apt install apache2
systemctl enable apache2
systemctl restart apache2
Configurations:
Main: /etc/apache2/apache2.conf
Apache environment vars: /etc/apache2/envvars
All configured sites: /etc/apache2/sites-available
All enabled sites (Must be configured 1st) : /etc/apache2/sites-enabled
All modules available : /etc/apache2/mods-available/
All enabled modules : /etc/apache2/mods-enabled
Any change in configuration must be follow by restarting the apache2 to take effect.
24
Apache Web Server 6-Apache main server
configurations
/etc/apache2/apache2.conf
ServerRoot : Defines the directory contains all configuration files for apache
PidFile : File contains the PID for the master apache process
User, Group : The user and group the apache pre-fork process will start with.
Include and IncludeOptional : During parsing phase, merge the requested conf file
ErrorLog : Name of error log file. If it is relative path, it will be under ServerRoot
/etc/apache2/ports.conf
Listen : Configure the socket that apache will use. 25
Apache Web Server 7-Directory directive,
options, security.
Used to add group of directives to apply for:
Certain directory
Subdirectories
Files
Order allow, deny : Order of security restrictions based on the client L3 address. The
traffic will be tested by the 1st directive, and the rest will be matched by the 2nd.
26
Apache Web Server 7-Directory directive,
options, security.
Example:
<Directory /var/www/iti>
Options +Indexes
AllowOverride None
Order allow,deny
Allow from 172.16.0.0/16
</Directory>
Directory is /var/www/iti
Allow the specified address specified, and then deny all others.
If a directive from .htaccess conflicts with main server configuration, will return to
AllowOverride.
28
Apache Web Server 8-Apache
authentication.
How to secure part or all of web by username/password.
Can be configured in .conf files or in .htaccess
There are many back-end authentication DBs for apache as (MySQL, LDAP, Files)
Supports many type of authenticaton as (basic, form, digest )
Can restrict valid users to certain user,all , or group of users.
Example:
AuthType Basic
AuthName “Private area’
AuthUserFile /var/www/iti/.htpasswd
Require valid-user
29
Apache Web Server 9-Apache modules,
mod_rewrite
Enable to disable apache module needs to restart apache2 service.
RewriteEngine on , turn the engine on, so the rules would take effects
RewriteEngine off, turn the engine off, so the rules would not take effects.
Uses RegEx. 30
Apache Web Server 9-Apache modules,
mod_rewrite
RewriteRule
RewriteRule Pattern Substitution [Flags]
If the RewriteCond test returns true, it will execute all the incoming RewriteRules till
we get a RewriteCond, or end of RewriteCond/RewriteRule, or RewriteRule with [L]
32
Apache Web Server 9-Apache modules,
mod_rewrite
RewriteCond
RewriteCond %{QUERY_STRING} "noha“
RewriteRule will replace all the url with the new string and terminates the
URL path with ? To remove the QUERY_STRING.
33
Apache Web Server 10-Virtual Hosts
The term VirtualHost refers to that run multiple web sites on the same web server.
The web server will differentiate between requests using the Host field in HTTP request
<Directory /var/ahmed.com>
Require all granted
</Directory>
For site ahmed.com, create a file /etc/apache2/sites-available/Mohamed.com.conf
<VirtualHost *:80>
ServerName Mohamed.com
DocumentRoot /var/Mohamed.com
</VirtualHost>
<Directory /var/ahmed.com>
Require all granted
</Directory>
36
Apache Web Server 10-Virtual Hosts
37
Apache Web Server 11-Encrypting Apache
traffic using HTTPS
HTTP protocol is a plain text protocol by nature.
Enable ssl module to encrypt the packet payload, use https protocol, and port 443.How?
How to use rewrite module to convert any request from http to https?
Install php
sudo apt install php php-mysql
sudo systemctl restart apache2
Edit file index.php in DocumentRoot and add the following lines
<?php
phpinfo();
?> 39
Apache Web Server 12-php with Apache
40
Apache Web Server 12-php with Apache
php.ini contains all PHP settings.
Try php –i
41
Apache Web Server 12-php with Apache
Phpmyadmin project, is an open source web application.
42
Apache Web Server 12-php with Apache
Composer:
Dependency manager for PHP
Pull all in all required libraries, dependencies, and manage all in one place.
PEAR established from long time but abandoned by many php devs.
Install
curl -s https://getcomposer.org/installer | php
Use json files to install any needed package.
To install slim framework, create a file composer.json
{
"require": {
“slim/slim": “3.0.*"
}
}
43
php composer.phar install