XP-rience

Something that I'd like to share with you!

Monday, April 20, 2020

Nginx PHP-FPM setup under Centos 7

No comments :
Nginx (pronounced as Engine-X) is a web and reverse proxy server well known for its speed and capability to handle large number of requests

While PHP-FPM (PHP-FastCGI process manager) is a common gateway interface between the web server and dynamic content serving programs. It listens on a port or socket and passes the request between the PHP and web server

To start with, install Nginx...

[root@localhost ~]# yum install nginx -y

Start the service

[root@localhost ~]# service nginx start

Now test the Nginx by opening its default web page http://<your host>/



Find Nginx web directory by checking service status

[root@localhost ~]# service nginx status

Apr 20 20:42:56 localhost.localdomain nginx[5942]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok

Apr 20 20:42:56 localhost.localdomain nginx[5942]: nginx: configuration file /etc/nginx/nginx.conf test is successful

Grep "root" from the "nginx.conf" file

[root@localhost ~]# grep root /etc/nginx/nginx.conf
root         /usr/share/nginx/html;

Create a PHP test page "test.php" as content below and save it to /usr/share/nginx/html/

<?php
phpinfo();
?>

Try to run the PHP file = http://<your host>/test.php

As we know it won't parse. It will just download or show the PHP file content. This is expected since we didn't link Nginx with PHP-FPM yet.

Install PHP-FPM

[root@localhost ~]# yum install php-fpm -y

Check the PHP-FPM installation

[root@localhost ~]# service php-fpm status

Locate the PHP=FPM "www.conf" file

[root@localhost ~]# locate www.conf
/etc/php-fpm.d/www.conf

Edit the "/etc/php-fpm.d/www.conf" change listen ip:port to socket

; The address on which to accept FastCGI requests.
; Valid syntaxes are:
;   'ip.add.re.ss:port'    - to listen on a TCP socket to a specific address on
;                            a specific port;
;   'port'                 - to listen on a TCP socket to all addresses on a
;                            specific port;
;   '/path/to/unix/socket' - to listen on a unix socket.
; Note: This value is mandatory.
;listen = 127.0.0.1:9000
listen = /run/php-fpm/php-fpm.sock

Restart PHP-FPM

[root@localhost ~]# service php-fpm restart

Backup and edit /etc/nginx/nginx.conf
Under server scope {} block, add location scope {} block to enable PHP-FPM to parsePHP file

server {
listen       80 default_server;
listen       [::]:80 default_server;
server_name  _;
root         /usr/share/nginx/html;

# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;

#location / {
#}

location ~* \.php$ {
fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
include         fastcgi_params;
fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
}

error_page 404 /404.html;
location = /40x.html {
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
}

}

Make sure this line is pointing to correct Unix socket

fastcgi_pass unix:/run/php-fpm/php-fpm.sock;


Restart both Nginx and PHP-FPM

[root@localhost ~]# service nginx restart
Redirecting to /bin/systemctl restart nginx.service

[root@localhost ~]# service php-fpm restart
Redirecting to /bin/systemctl restart php-fpm.service

Try to run the PHP file = http://<your host>/test.php again





x

Sunday, April 19, 2020

Minimal PHP/Apache Docker setup

No comments :

Why Docker?

The portability of the containers, can be deployed anywhere
Efficient system resource usage compare to VM

How to Dockerize your php application?

Assuming your OS already have a good and working Docker installation
Let us use single page PHP applications below, index.php inside a webfolder directory
index.php content;

<?php
phpinfo();
?>

[root@localhost php_docker]# tree .
.
└── webfolder
└── index.php

Create a Dockerfile

Dockerfile in a list of instruction/command call to assemble the image

[root@localhost php_docker]# tree .
.
├── Dockerfile
└── webfolder
└── index.php

Content of the Dockerfile;

FROM php:7.4-apache
COPY webfolder/ /var/www/html/
EXPOSE 80

Explanation for each line

FROM php:7.4-apache
  • php:7.4-apache based image selection
COPY webfolder/ /var/www/html/
  • copy webfolder content into container /var/www/html/
  • Apache2 default web directory
EXPOSE 80
  • expose port 80 to the host
Next lets build the docker image

[root@localhost php_docker]# docker build --rm -t myphpdocker .

docker build [OPTIONS] PATH | URL | -
--rm = Remove intermediate containers after a successful build
-t = Name and optionally a tag in the 'name:tag' format
myphpdocker = docker container name
. (single dot) = build path

Check the built image

[root@localhost php_docker]# docker image ls

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
myphpdocker         latest              0c1fe5b1aa9e        47 minutes ago      414 MB

Next is to run the docker

docker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG...]
-t = Allocate a pseudo-TTY
-i = Keep STDIN open even if not attached
-it = For interactive processes (like a shell), you must use -i -t together
-p = Publish a container's port(s) to the host
-d = Run container in background and print container ID

Lets try the interactive mode first

[root@localhost php_docker]# docker run -p 80:80 -it myphpdocker

Apache log appear on the terminal.
Open your browser and head to "http:/<host ip>/"


Now try the detach mode

[root@localhost php_docker]# docker run -p 80:80 -d myphpdocker

Check the running container

[root@localhost php_docker]# docker ps

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
4619304831d4        myphpdocker         "docker-php-entryp..."   6 seconds ago       Up 5 seconds        0.0.0.0:80->80/tcp   frosty_tesla

Wednesday, April 15, 2020

vLan on Realtek PCIe GBE Family

No comments :
Download "Diagnostic Program for Win7/Win8/Win10" and install

https://www.realtek.com/en/component/zoo/category/network-interface-controllers-10-100-1000m-gigabit-ethernet-pci-express-software


Select the Realtek NIC > VLAN and add accordingly