![Page 1: Linux Network Servers nginx, PHP-FPM, MySQL Rostislav Skudnov, Timo Jääskeläinen](https://reader036.vdocuments.site/reader036/viewer/2022062408/56649efb5503460f94c0e3b9/html5/thumbnails/1.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062408/56649efb5503460f94c0e3b9/html5/thumbnails/2.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062408/56649efb5503460f94c0e3b9/html5/thumbnails/3.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062408/56649efb5503460f94c0e3b9/html5/thumbnails/4.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062408/56649efb5503460f94c0e3b9/html5/thumbnails/5.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062408/56649efb5503460f94c0e3b9/html5/thumbnails/6.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062408/56649efb5503460f94c0e3b9/html5/thumbnails/7.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062408/56649efb5503460f94c0e3b9/html5/thumbnails/8.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062408/56649efb5503460f94c0e3b9/html5/thumbnails/9.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062408/56649efb5503460f94c0e3b9/html5/thumbnails/10.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062408/56649efb5503460f94c0e3b9/html5/thumbnails/11.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062408/56649efb5503460f94c0e3b9/html5/thumbnails/12.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062408/56649efb5503460f94c0e3b9/html5/thumbnails/13.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062408/56649efb5503460f94c0e3b9/html5/thumbnails/14.jpg)
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'")