Trong bài này, chúng ta sẽ dùng hostname kenhgiaiphap.vn với địa chỉ IP 192.168.11.230. Bên cạnh đó, php-fpm không có sẵn trên repository chính thức của CentOS, nhưng lại có trên Remi RPM với cơ chế phụ thuộc vào EPEL repository, chúng ta có thể kích hoạt cả 2 repository như sau:
rpm –import https://fedoraproject.org/static/0608B895.txt
.png)
rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
yum install yum-priorities
Chỉnh sửa file /etc/yum.repos.d/epel.repo…
vi /etc/yum.repos.d/epel.repo
Và thêm dòng priority=10 vào phần [epel]:
.png)
Làm tương tự như vậy với phần [remi] trong /etc/yum.repos.d/remi.repo, và thay đổi enabled thành 1:
vi /etc/yum.repos.d/remi.repo
.png)
Installing MySQL 5
yum install mysql mysql-server
.png)
Cho MYSQL Khởi động cùng hệ thống và start Mysql
chkconfig –levels 235 mysqld on
/etc/init.d/mysqld start
Kiểm tra Xem MYSQL đã start chưa
netstat -tap | grep mysql
Nếu hiện như sau là ok
[root@kenhgiaiphap ~]# netstat -tap | grep mysql
tcp 0 0 *:mysql *:* LISTEN 5069/mysqld
Thiết lập password cho user root : (Vidu: 123456 )
/usr/bin/mysqladmin -u root password ‘123456’
Installing Nginx
yum install nginx
Cho Nginx khởi động cùng hệ thống và start Nginx
chkconfig –levels 235 nginx on
/etc/init.d/nginx start
Sau đó, gõ địa chỉ IP của web server vào trình duyệt (ví dụ http://192.168.11.230) sẽ có giao diện như sau :
.png)
Installing PHP5
yum install php-fpm php-cli php-mysql php-gd php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-eaccelerator php-magickwand php-magpierss php-mbstring php-mcrypt php-mssql php-shout php-snmp php-soap php-tidy
Sau đó, mở file /etc/php.ini và thiết lập cgi.fix_pathinfo=0:
vi /etc/php.ini
.png)
Bạn có thể kham khảo thêm tại :
http://wiki.nginx.org/Pitfalls
Trong /var/log/php-fpm/www-error.log khi chúng ta tiến hành gọi mã PHP trong trình duyệt, các bạn hãy thiết lập date.timezone trong file /etc/php.ini như sau:
[…]
[Date]
; Defines the default timezone used by the date functions
; http://www.php.net/manual/en/datetime.configuration.php#ini.date.timezone
date.timezone = “Asia/Ho_Chi_Minh”
[…]
.png)
Để tìm timezone của hệ thống ta dùng lệnh
cat /etc/sysconfig/clock
.png)
Cho php-fpm khởi động cùng hệ thống và start php-fpm
chkconfig –levels 235 php–fpm on
/etc/init.d/php–fpm start
PHP-FPM là 1 tiến trình daemon (với đoạn script /etc/init.d/php-fpm) có cơ chế hoạt động cùng với FastCGI server trên cổng 9000.
Configuring nginx
File thiết lập, cấu hình chính của nginx là /etc/nginx/nginx.conf
Kham khảo thêm tại
http://wiki.codemongers.com/NginxFullExample
và
http://wiki.codemongers.com/NginxFullExample2
Trước tiên, chúng ta có thể tăng số lượng các tiến trình xử lý và thiết lập thông số keepalive_timeout về 1 giá trị phù hợp:
vi /etc/nginx/nginx.conf
#######################################################################
#
# This is the main Nginx configuration file.
#
# More information about the configuration options is available on
# * the English wiki – http://wiki.nginx.org/Main
# * the Russian documentation – http://sysoev.ru/nginx/
#
#######################################################################
#———————————————————————-
# Main Module – directives that cover basic functionality
#
# http://wiki.nginx.org/NginxHttpMainModule
#
#———————————————————————-
user nginx;
worker_processes 4;
error_log /var/log/nginx/error.log;
#error_log /var/log/nginx/error.log notice;
#error_log /var/log/nginx/error.log info;
pid /var/run/nginx.pid;
#———————————————————————-
# Events Module
#
# http://wiki.nginx.org/NginxHttpEventsModule
#
#———————————————————————-
events {
worker_connections 1024;
}
#———————————————————————-
# HTTP Core Module
#
# http://wiki.nginx.org/NginxHttpCoreModule
#
#———————————————————————-
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main ‘$remote_addr – $remote_user [$time_local] “$request” ‘
‘$status $body_bytes_sent “$http_referer” ‘
‘”$http_user_agent” “$http_x_forwarded_for”‘;
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
#
# The default server
#
server {
listen 80;
server_name _;
# dòng server_name _; là do làm trên localhost
# khi làm thực tế thì chỉnh lại cho phù hợp vidu:
#server {
# listen 80;
# server_name kenhgiaiphap.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm index.php;
}
error_page 404 /404.html;
location = /404.html {
root /usr/share/nginx/html;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache’s document root
# concurs with nginx’s one
#
location ~ /\.ht {
deny all;
}
}
# Load config files from the /etc/nginx/conf.d directory
# include /etc/nginx/conf.d/*.conf;
}
.png)
Thông số server_name _; sẽ biến phần này thành vhost mặc định (tuy nhiên, người sử dụng vẫn phải chỉ định rõ ràng giá trị hostname tương ứng, ví dụ như www.example.com). Ở chế độ mặc định, PHP-FPM sẽ luôn lắng nghe các tín hiệu trên cổng 9000, do vậy chúng ta phải thiết lập nginx kết nối tới 127.0.0.1:9000 đi kèm với fastcgi_pass 127.0.0.1:9000.
Sau đó, khởi động lai nginx:
/etc/init.d/nginx restart
Tạo File info.php trong /usr/share/nginx/html/
vi /usr/share/nginx/html/info.php
file info.php có nội dung sau:
phpinfo();
?>
Kiểm tra xem Php đã hoạt động chưa :
Vào trình duyệt gõ (http://192.168.11.230/info.php):
.png)
Chúng ta có thể thấy rằng PHP5 đã làm việc, và hoạt động qua FPM/FastCGI, được chỉ ra tại dòng Server API. Nếu kéo chuột xuống phía dưới, chúng ta sẽ thấy tất cả các module đã được kích hoạt sẵn trong PHP5, trong đó có bao gồm cả MySQL:
.png)
Thiết lập PHP-FPM sử dụng Unix Socket
Mặt khác, PHP – FPM luôn “lắng nghe” các tín hiệu trên cổng 9000 qua địa chỉ 127.0.0.1 ở chế độ mặc định, và chúng ta cũng có thể thiết lập PHP-FPM sử dụng socket Unix để tránh hiện tượng quá tải TCP.
Để thực hiện, Ta mở file cấu hình /etc/php-fpm.d/www.conf…
vi /etc/php-fpm.d/www.conf
thay đổi dòng listen như bên dưới :
[…]
;listen = 127.0.0.1:9000
listen = /tmp/php5-fpm.sock
[…]
.png)
Khởi động lại PHP-FPM:
/etc/init.d/php–fpm restart
Tiếp theo, tìm kiếm trong file cấu hình nginx với tất cả các giá trị của vhosts, thay đổi fastcgi_pass 127.0.0.1:9000; thành fastcgi_pass unix:/tmp/php5-fpm.sock;
vi /etc/nginx/nginx.conf
[…] location ~ \.php$ {root /usr/share/nginx/html;
fastcgi_pass unix:/tmp/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
[…]
Cuối cùng, khởi động lại Nginx:
/etc/init.d/nginx restart
Một số Link cần kham khảo :
Nginx
nginx Wiki
PHP_FPM
(Có kham khảo bài viết bên Quantrimang) !
Phan trọng Hiếu (theo Howtoforge.com)