Bài đăng nổi bật

Redo log, undo log và binary log

Đây là ba loại log mà bạn đã từng nghe khi tiếp cận mysql. Trong các cơ sở dữ liệu quan hệ (RDBMS) khác, cũng sẽ có các thành phần có vai tr...

Thứ Năm, 18 tháng 2, 2016

Cấu hình phpMyAdmin

PhpMyAdmin là một database management cho mysql trên nền web được viết bằng PHP.

Để cấu hình được nó bạn phải có:

  • Web server. Tôi chọn nginx
  • CGI để biên dịch php. Tôi chọn php-fpm
  • MySQL và MySQL driver cho php
  • Cuối cùng là phpmyadmin.

Cài đặt

yum install nginx php-fpm php-mysql mysql

Mặc định cài từ base thì phiên bản php sẽ là 5.3.3 và mysql 5.5 Bản download phpMyAdmin có yêu cầu về phiên bản php và mysql sử dụng nên bạn cần xem kỹ trong

https://www.phpmyadmin.net/downloads/

Theo release note và phiên bản php, mysql trên server, tôi chọn bản phpMyAdmin 4.4.15.4
https://www.phpmyadmin.net/files/4.4.15.4/

wget --no-check-certificate https://files.phpmyadmin.net/phpMyAdmin/4.0.10.14/phpMyAdmin-4.0.10.14-english.tar.gz

tar xvzf phpMyAdmin-4.0.10.14-english.tar.gz
mv phpMyAdmin-4.0.10.14-english.tar.gz phpMyAdmin

Cấu hình nhanh

cp -r phpMyAdmin/ /usr/share/nginx/html/

chown -R nginx:nginx /usr/share/nginx/html/phpMyAdmin
find /usr/share/nginx/html/phpMyAdmin -type f -exec chmod 644 {} \;
find /usr/share/nginx/html/phpMyAdmin -type d -exec chmod 755 {} \;

vim /etc/nginx/conf.d/default.conf

Bỏ comment các dòng sau:

    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;
    }

Sửa dòng
  fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
thành
  fastcgi_param  SCRIPT_FILENAME  $document_root/$fastcgi_script_name;

Mặc định php-fpm dùng tcp socket listen trên 127.0.0.1:9000 nên bạn không cần chỉnh sửa cấu hình gì cho php-fpm

service nginx restart
service php-fpm restart
service mysqld restart

Chạy mysql_secure_installation để khởi tạo passwd cho root.

Truy cập vào:
http://<server_name hoặc ip>/phpMyAdmin/index.php

Nhập tài khoản mysql root vào là xong. Thường phpMyAdmin sẽ được deploy riêng trên một server khác có cùng LAN với các DB server. Server PhpMyAdmin này có thể có wan để truy cập từ ngoài hoặc cài đặt truy cập qua VPN.

Tạo vhost cho phpMyAdmin

Trong cách cấu hình nhanh ở trên, tôi đưa phpMyAdmin thành một thư mục trong default virtualhost của nginx. Trong cách này, tôi sẽ cấu hình phpMyAdmin riêng trên một Vhost.

vim /etc/nginx/conf.d/phpMyAdmin.example.com.conf
server{

     listen       80;
     server_name phpMyAdmin.example.com www.phpMyAdmin.example.com;
     root /var/www/phpMyAdmin;
     error_log  /var/log/nginx/phpMyAdmin.example.com_error.log error;
     access_log  /var/log/nginx/phpMyAdmin.example.com_access.log  main;
     location /{
         index index.html index.php;
     }

     location ~ \.php {
         fastcgi_pass 127.0.0.1:9000;
         fastcgi_index   index.php;
         include         /etc/nginx/fastcgi_params;
         fastcgi_param   SCRIPT_FILENAME $document_root/$fastcgi_script_name;
     }
}
useradd -c "phpMyAdmin user" -s /sbin/nologin -d /var/www/phpMyAdmin phpMyAdmin

chown -R phpMyAdmin:phpMyAdmin /var/www/phpMyAdmin
find /var/www/phpMyAdmin -type f -exec chmod 644 {} \;
find /var/www/phpMyAdmin -type d -exec chmod 755 {} \;

Chỉnh file /etc/hosts trên máy client để phân giải phpMyAdmin.example.com sau đó truy cập vào
http://phpMyAdmin.example.com/ bạn sẽ thấy form login quen thuộc lại hiện ra

Siết chặt an toàn cho phpMyAdmin

Bạn có thể bổ sung authentication cho khu vhost phpMyAdmin.example.com hoặc location /phpMyAdmin nếu làm theo cách cấu hình nhanh

openssl passwd
một encrypted passwd được gen ra

vim /etc/nginx/.user_passwd
admin:<encrypted passwd vừa gen ra từ openssl util>

Vì passwd này đã bị mã hóa nên có ai đó đọc được file .user_passwd cũng không thể biết được passwd thực sự của admin user

vim /etc/nginx/conf.d/phpMyAdmin.example.com.conf
Bổ sung vào location /
auth_basic "secret site";
auth_basic_user_file /etc/nginx/.user_passwd;

restart server nginx

Config phpMyAdmin

Bản thân một phpMyAdmin có thể hỗ trợ multi db, nghĩa là bạn không cần tốn công tạo nhiều vhost phpMyAdmin cho mỗi db.

cp /var/www/phpMyAdmin/config.sample.inc.php  /var/www/phpMyAdmin/config.inc.php
vim /var/www/phpMyAdmin/config.inc.php

Chú ý đoạn config Servers thôi:
Danh sách server được đánh số từ 1, điều khiển bởi một biến $i

$i = 0;

/*
 * First server
 */
$i++;
/* Authentication type */
$cfg['Servers'][$i]['auth_type'] = 'cookie';
/* Server parameters */
$cfg['Servers'][$i]['host'] = 'localhost';
$cfg['Servers'][$i]['connect_type'] = 'tcp';
$cfg['Servers'][$i]['compress'] = false;
/* Select mysql if your server does not have mysqli */
$cfg['Servers'][$i]['extension'] = 'mysqli';
$cfg['Servers'][$i]['AllowNoPassword'] = false;
$cfg['Servers'][$i]['AllowRoot'] = false;
$cfg['Servers'][$i]['hide_db'] = '(mysql|test|information_schema|performance_schema)';
$cfg['Servers'][$i]['verbose'] = 'This host';
/*
 * End of first servers configuration
 */

/*
 * Second server
 */
$i++;
/* Authentication type */
$cfg['Servers'][$i]['auth_type'] = 'cookie';
/* Server parameters */
$cfg['Servers'][$i]['host'] = '1.2.3.4';
$cfg['Servers'][$i]['connect_type'] = 'tcp';
$cfg['Servers'][$i]['compress'] = false;
/* Select mysql if your server does not have mysqli */
$cfg['Servers'][$i]['extension'] = 'mysqli';
$cfg['Servers'][$i]['AllowNoPassword'] = false;
$cfg['Servers'][$i]['AllowRoot'] = false;
$cfg['Servers'][$i]['hide_db'] = '(mysql|test|information_schema|performance_schema)';
$cfg['Servers'][$i]['verbose'] = 'Remote host';
/*
 * End of second servers configuration
 */

Muốn thêm bao nhiêu server bạn chỉ việc add thêm bấy nhiêu đoạn config. Trong đoạn config server có vài điểm chú ý

$cfg['Servers'][$i]['auth_type'] có thể nhận các giá trị config, cookie hoặc http. Với giá trị config, đây là cách cấu hình cũ, thông tin xác thực sẽ phải khai báo trong config.inc.php. Cookie và http là các cách cấu hình mới, khi mà phpMyAdmin sẽ dùng luôn account trong mysql để đăng nhập. Tuy vậy, passwd khi được truyền đi vẫn là plain text trừ khi sử dụng https. Cookie mode, password được lưu trong temporary cookie và được mã hóa. Cookie mode là auth_type mặc định hiện nay.

$cfg['Servers'][$i]['host'] có thể nhận ip hoặc domain name. Trường hợp bạn để trống '' thì có nghĩa bạn đang disable server này từ login form của phpmyadmin

$cfg['Servers'][$i]['AllowNoPassword'] = false;
$cfg['Servers'][$i]['AllowRoot'] = false;

Là hai config siết chặt an toàn cho login qua phpMyAdmin

$cfg['Servers'][$i]['hide_db'] = '(mysql|test|information_schema|performance_schema)';
Với cấu hình này, các db của hệ thống sẽ bị ẩn đi khỏi phpMyAdmin

Bạn có thể dùng $cfg['Servers'][$i]['only_db'] để chỉ show ra một số nhất định db

$cfg['Servers'][$i]['verbose'] = 'Remote host'; Mặc định nếu không set verbose. Server choice trong login form của phpMyAdmin sẽ hiện thông tin host.

Nhưng sử dụng verbose, thông tin này sẽ bị thay thế


Nếu bạn đã sử dụng verbose, bản phải dùng verbose trên tất cả các config server để tránh báo lỗi. Mọi chỉnh sửa trong config.inc.php không yêu cầu phải restart service nginx hay php-fpm.

Không có nhận xét nào:

Đăng nhận xét