Linux Network Servers nginx, PHP-FPM, MySQL Rostislav Skudnov, Timo Jääskeläinen.

14
Linux Network Servers nginx, PHP-FPM, MySQL Rostislav Skudnov, Timo Jääskeläinen

Transcript of Linux Network Servers nginx, PHP-FPM, MySQL Rostislav Skudnov, Timo Jääskeläinen.

Page 1: Linux Network Servers nginx, PHP-FPM, MySQL Rostislav Skudnov, Timo Jääskeläinen.

Linux Network Serversnginx, PHP-FPM, MySQL

Rostislav Skudnov, Timo Jääskeläinen

Page 2: Linux Network Servers nginx, PHP-FPM, MySQL Rostislav Skudnov, Timo Jääskeläinen.

What is nginx?

This Russian guy has created the 3rd most popular Web server

Used on WordPress, Hulu,  SourceForge, and many more...

Source: http://news.netcraft.com/archives/2010/04/15/april_2010_web_server_survey.html

Igor Sysoev

Page 3: Linux Network Servers nginx, PHP-FPM, MySQL Rostislav Skudnov, Timo Jääskeläinen.

What is PHP-FPM?FastCGI Process Manager for PHP

Another Russian guy, who wasn't satisfied with existing PHP FastCGI, and has created FPM in his spare time

In PHP core since 5.5.3

Andrei Nigmatulin

Page 4: Linux Network Servers nginx, PHP-FPM, MySQL Rostislav Skudnov, Timo Jääskeläinen.

Classic LAMP vs nginx + PHP-FPM

LAMP One Apache process per each connection

PHP is compiled into Apache and launches again on every request

 High memory and CPU consumption in a high load environment

nginx + PHP-FPM A few nginx processes handle thousands of connections

PHP processes are spawned when necessary; already spawned ones are reused

Low memory and CPU consumption

Page 5: Linux Network Servers nginx, PHP-FPM, MySQL Rostislav Skudnov, Timo Jääskeläinen.

Installing nginx

wget http://nginx.org/download/nginx-0.9.4.tar.gztar -xzf nginx-0.9.4.tar.gzcd nginx-0.9.4./configure --with-http_ssl_module ...makemake install

Create init script in /etc/init.d/nginx

Page 6: Linux Network Servers nginx, PHP-FPM, MySQL Rostislav Skudnov, Timo Jääskeläinen.

Installing PHP-FPM

wget http://www.php.net/get/php-5.3.5.tar.gztar -xzf php-5.3.5.tar.gzcd php-5.3.5 ./configure --enable-fpm --with-mysql --with-mcrypt --with-curl --with-mysqli --with-mysql-sock --with-pdo-mysql --with-gd --with-zlibmakemake install

Create init script in /etc/init.d/php-fpm

Page 7: Linux Network Servers nginx, PHP-FPM, MySQL Rostislav Skudnov, Timo Jääskeläinen.

Configuring nginx

less /usr/local/nginx/conf/nginx.confuser www-data;worker_processes  1;

error_log  /var/log/nginx/error.log;pid        /var/run/nginx.pid;

events {    worker_connections  1024;}

http {    include       mime.types;    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    gzip  on;

    include /usr/local/nginx/sites-enabled/*;

}

Page 8: Linux Network Servers nginx, PHP-FPM, MySQL Rostislav Skudnov, Timo Jääskeläinen.

The add and remove PHP scripts

addclient.php:Creates Linux user with auto generated passwordWrites Nginx configuration fileWrites FPM configuration fileCreates a placeholder web page for the new userCreates MySQL user and database

remclient.php:Reverse action – removes everything including user dir

Page 9: Linux Network Servers nginx, PHP-FPM, MySQL Rostislav Skudnov, Timo Jääskeläinen.

Creating Linux userThe PHP script:•Generates a random password•Makes sure that user name and domain names are correct (RegEx)•Sends command to create the user

// Creating Unix user, default group www-datapassthru("useradd -m -K UMASK=027 -s /bin/bash -c '$domain' -p $passwordCrypted $username", $res);passthru("usermod -a -G $username www-data");

•In the end script checks whether the user was created successfully or not

Page 10: Linux Network Servers nginx, PHP-FPM, MySQL Rostislav Skudnov, Timo Jääskeläinen.

Generating Nginx and FPM configs

The PHP script:

•Uses pre-written templates for both configs, replacing username and domain info

•Copies the config files into nginx available-sites and fpm config directory

•Creates a link into Nginx sites-enabled

Page 11: Linux Network Servers nginx, PHP-FPM, MySQL Rostislav Skudnov, Timo Jääskeläinen.

Configuring nginx - vhost template

less ~/hostadmin/nginx_tpl.txtserver { listen 80; server_name [[domain]]; root /home/[[user]]/www/; access_log /home/[[user]]/logs/access.log; error_log /home/[[user]]/logs/error.log; location / { try_files $uri $uri/ @rewr;

index index.html index.php; } location ~ \.php$ { include /usr/local/nginx/conf/fastcgi.conf; fastcgi_pass unix:/tmp/[[user]].sock; fastcgi_index index.php; } location @rewr { rewrite ^/(.*)$ /index.php?q=$1 last; }}

Page 12: Linux Network Servers nginx, PHP-FPM, MySQL Rostislav Skudnov, Timo Jääskeläinen.

Configuring PHP-FPM - vhost template

less ~/hostadmin/fpm_tpl.txt[[[user]]]

listen = /tmp/[[user]].sock

user = [[user]]group = [[user]]

pm = dynamicpm.max_children = 50pm.start_servers = 1pm.min_spare_servers = 1pm.max_spare_servers = 35

php_admin_value[error_log] = /home/[[user]]/logs/fpm-php.log

Page 13: Linux Network Servers nginx, PHP-FPM, MySQL Rostislav Skudnov, Timo Jääskeläinen.

Creating a placeholder web page

The PHP script:

•Uses pre-written HTML and replacing user name and domain name

•Copies the new HTML file into user’s www directory

•Sets permissions

Page 14: Linux Network Servers nginx, PHP-FPM, MySQL Rostislav Skudnov, Timo Jääskeläinen.

Creating the MySQL database

The PHP script uses three simple MySQL:

Create database:

mysql_query("create database $user")

Create user with generated password and all privileges to his/her database:

mysql_query("grant all privileges on $user.* to $user@localhost identified by '$pass'")