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: b_server_survey.html Igor Sysoev
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 Andrei Nigmatulin
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
Installing nginx wget tar -xzf nginx tar.gz cd nginx /configure --with-http_ssl_module... make make install Create init script in /etc/init.d/nginx
Installing PHP-FPM wget tar -xzf php tar.gz cd php /configure --enable-fpm --with-mysql --with-mcrypt --with-curl --with- mysqli --with-mysql-sock --with-pdo-mysql --with-gd --with-zlib make make install Create init script in /etc/init.d/php-fpm
Configuring nginx less /usr/local/nginx/conf/nginx.conf user 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/*; }
The add and remove PHP scripts addclient.php: Creates Linux user with auto generated password Writes Nginx configuration file Writes FPM configuration file Creates a placeholder web page for the new user Creates MySQL user and database remclient.php: Reverse action – removes everything including user dir
Creating Linux user The 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-data passthru("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
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
Configuring nginx - vhost template less ~/hostadmin/nginx_tpl.txt server { 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 index index.html index.php; } location ~ \.php$ { include /usr/local/nginx/conf/fastcgi.conf; fastcgi_pass unix:/tmp/[[user]].sock; fastcgi_index index.php; } { rewrite ^/(.*)$ /index.php?q=$1 last; }
Configuring PHP-FPM - vhost template less ~/hostadmin/fpm_tpl.txt [[[user]]] listen = /tmp/[[user]].sock user = [[user]] group = [[user]] pm = dynamic pm.max_children = 50 pm.start_servers = 1 pm.min_spare_servers = 1 pm.max_spare_servers = 35 php_admin_value[error_log] = /home/[[user]]/logs/fpm-php.log
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
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 identified by '$pass'")