xay dung vps nginx update

23
QUYLEVHB.BLOGSPOT.COM 2015 Q CÀI ĐẶT VPS A. Các thành phần sẽ cài - Cài đặt Nginx, ngx_pagespeed và cấu hình tối ưu - Cài đặt và tối ưu Mariadb - Cài đặt và tối ưu php-fpm - Phpmyadmin - VSFTPD - Xcache - CSF - Bonus : Bảo mật nginx cơ bản B. Các bước chuẩn bị. I. Add các repo cần thiết rpm -Uvh http://download.fedoraproject.org/pub/epel/6/x86_64/epel- release-6-8.noarch.rpm rpm -Uvh http://rpms.famillecollet.com/enterprise/remi- release-6.rpm rpm -Uvh http://www6.atomicorp.com/channels/atomic/centos/6/i386/RPMS /atomic-release-1.0-19.el6.art.noarch.rpm BLOG HACKING & SECURITY: QUYLEVHB.BLOGSPOT.COM Page 1

Upload: la-dai-hiep

Post on 17-Jan-2016

37 views

Category:

Documents


0 download

DESCRIPTION

Xay Dung Vps Nginx Update

TRANSCRIPT

Page 1: Xay Dung Vps Nginx Update

Q

CÀI Đ T VPSẶA. Các thành phần sẽ cài

- Cài đặt Nginx, ngx_pagespeed và cấu hình tối ưu - Cài đặt và tối ưu Mariadb- Cài đặt và tối ưu php-fpm- Phpmyadmin- VSFTPD- Xcache- CSF- Bonus : Bảo mật nginx cơ bản

B. Các bước chuẩn bị.I. Add các repo cần thiết

rpm -Uvh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpmrpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpmrpm -Uvh http://www6.atomicorp.com/channels/atomic/centos/6/i386/RPMS/atomic-release-1.0-19.el6.art.noarch.rpm

II. Cài các libs cần thiết

BLOG HACKING & SECURITY: QUYLEVHB.BLOGSPOT.COM Page 1

2015QUYLEVHB.BLOGSPOT.COM

Page 2: Xay Dung Vps Nginx Update

Q

yum -y install gcc-c++ pcre-dev pcre-devel zlib-devel make openssl-devel

III. Tạo user cho nginx

groupadd nginx

useradd -g nginx -d /dev/null -s /sbin/nologin nginx

C. Tiến hành cài đặtI. Cài đặt nginx và ngx_pagespeed

Chạy lần lượt các lệnh sau

cd /opt

wget https://github.com/pagespeed/ngx_pagespeed/archive/release-1.9.32.2-beta.zip

unzip release-1.9.32.2-beta.zip

cd ngx_pagespeed-release-1.9.32.2-beta

wget https://dl.google.com/dl/page-speed/psol/1.9.32.2.tar.gz

tar -xvf 1.9.32.2.tar.gz

cd /opt

wget http://nginx.org/download/nginx-1.7.9.tar.gz

tar -xvf nginx-1.7.9.tar.gz

cd nginx-1.7.9

./configure --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --with-http_ssl_module --conf-path=/etc/nginx/nginx.conf --with-http_gzip_static_module --with-http_realip_module --group=nginx --user=nginx --pid-path=/var/run/nginx.pid --with-http_stub_status_module --add-module=/opt/ngx_pagespeed-release-1.9.32.2-beta

make

make install

1. Tạo file khởi động

Tải file sau đây về và quăng vào /etc/init.d

https://drive.google.com/file/d/0B5GXJ8MAIvQqRWZiNEl1aExuZzA/view?usp=sharing- Chmod cho file nginx

BLOG HACKING & SECURITY: QUYLEVHB.BLOGSPOT.COM Page 2

2015QUYLEVHB.BLOGSPOT.COM

Page 3: Xay Dung Vps Nginx Update

Q

chmod +x /etc/init.d/nginx

- Run tiếp các lệnh saumkdir -p /var/cache/ngx_pagespeedmkdir -p /var/log/nginxchown -R nginx:nginx /var/cache/ngx_pagespeedchown -R nginx:nginx /var/log/nginx

- Khởi động nginxservice nginx start

chkconfig --levels 235 nginx on

2. Cấu hình

Truy cập /etc/nginx tải 2 file sau về và chép đè vào

https://drive.google.com/file/d/0B5GXJ8MAIvQqOE5oNDZab3JpUWM/view?usp=sharing

https://drive.google.com/file/d/0B5GXJ8MAIvQqRDdPa1BEX2Nzb0U/view?usp=sharing

3. Tạo virtual host và thư mục public_html

- Tạo thư mục conf.d bằng lệnh sau hoặc có thể dùng winscp để tao:

mkdir -p /etc/nginx/conf.d

- Tạo thư mục public_html

mkdir -p /home/domain-của-bạn/public_htmlmkdir /home/domain-của-bạn/logschmod 777 /domain-của-bạn/logs

chown –R nginx:nginx /home/domain-của-bạn

- Truy cập vào thư mục conf.d và tạo một file bất kỳ với đuôi là .conf

ví dụ mình tạo file  quylevhb.conf

BLOG HACKING & SECURITY: QUYLEVHB.BLOGSPOT.COM Page 3

2015QUYLEVHB.BLOGSPOT.COM

Page 4: Xay Dung Vps Nginx Update

Q

server {     server_name www.domain-của-bạn;     rewrite ^(.*) http://domain-của-bạn-không-có-www $1 permanent;     }server {     listen   80;     access_log off;     error_log off;     # error_log/home/domain-của-bạn/error.log;     root /home/domain-của-bạn/public_html;  index index.php index.html index.htm;     server_name domain-của-bạn-không-có-www;location / {try_files $uri $uri/ /index.php?$args;          } include /etc/nginx/ngx_pagespeed.conf; # bật ngx_pagespeedlocation ~ \.php$ {        root  /home/domain-của-bạn/public_html;        fastcgi_pass   127.0.0.1:9000;        fastcgi_index  index.php;        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;        include        fastcgi_params;    }}}

Thay đường dẫn và server name của các bạn vào là được. Nếu các bạn muốn ghi log thì bỏ dấu # ở đoạn # error_log/home/domain-của-bạn/error.log; và sửa error_log off; thành error_log on;

- Khởi động lại nginx

service nginx restart

BLOG HACKING & SECURITY: QUYLEVHB.BLOGSPOT.COM Page 4

2015QUYLEVHB.BLOGSPOT.COM

Page 5: Xay Dung Vps Nginx Update

Q

II. Cài đặt mariadb1. Add repo

Vào /etc/yum.repos.d/ tạo file: mariadb.repo với nội dung

[mariadb]name = MariaDBbaseurl = http://yum.mariadb.org/10.1/rhel6-amd64gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDBgpgcheck=1

Chú ý: Ở thời điểm mình viết tut thì phiên bản MariDB mới nhất là 10.1 . Để kiểm tra phiên bản các bạn có thể truy cập http://yum.mariadb.org/ sau khi tìm được bản mới nhất thì thay link phiên bản đó vào đòng:

baseurl = http://yum.mariadb.org/10.1/rhel6-amd64

2. Cài đặt

yum -y install MariaDB-server MariaDB-client

BLOG HACKING & SECURITY: QUYLEVHB.BLOGSPOT.COM Page 5

2015QUYLEVHB.BLOGSPOT.COM

Page 6: Xay Dung Vps Nginx Update

Q

BLOG HACKING & SECURITY: QUYLEVHB.BLOGSPOT.COM Page 6

2015QUYLEVHB.BLOGSPOT.COM

Page 7: Xay Dung Vps Nginx Update

Q

3. Đặt mật khẩu và bảo mật mysql

Chạy lệnh:

service mysql startchkconfig mysql onmysql_secure_installation

BLOG HACKING & SECURITY: QUYLEVHB.BLOGSPOT.COM Page 7

2015QUYLEVHB.BLOGSPOT.COM

Page 8: Xay Dung Vps Nginx Update

Q

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!In order to log into MariaDB to secure it, we'll need the currentpassword for the root user.  If you've just installed MariaDB, andyou haven't set the root password yet, the password will be blank,so you should just press enter here.Enter current password for root (enter for none): [Bấm Enter]OK, successfully used password, moving on...Setting the root password ensures that nobody can log into the MariaDBroot user without the proper authorisation.Set root password? [Y/n] YNew password: [Nhập mật khẩu bạn muốn]Re-enter new password: [Nhập lại mật khẩu]Password updated successfully!Reloading privilege tables.. ... Success!By default, a MariaDB installation has an anonymous user, allowing anyoneto log into MariaDB without having to have a user account created forthem.  This is intended only for testing, and to make the installationgo a bit smoother.  You should remove them before moving into aproduction environment.Remove anonymous users? [Y/n] Y ... Success!Normally, root should only be allowed to connect from 'localhost'.  Thisensures that someone cannot guess at the root password from the network.Disallow root login remotely? [Y/n] Y ... Success!By default, MariaDB comes with a database named 'test' that anyone canaccess.  This is also intended only for testing, and should be removedbefore moving into a production environment.Remove test database and access to it? [Y/n] Y - Dropping test database... ... Success! - Removing privileges on test database... ... Success!Reloading the privilege tables will ensure that all changes made so farwill take effect immediately.Reload privilege tables now? [Y/n] Y ... Success!Cleaning up...All done!  If you've completed all of the above steps, your MariaDBinstallation should now be secure.Thanks for using MariaDB!

BLOG HACKING & SECURITY: QUYLEVHB.BLOGSPOT.COM Page 8

2015QUYLEVHB.BLOGSPOT.COM

Page 9: Xay Dung Vps Nginx Update

Q

4. Tối ưu MariaDB

mở file /etc/my.cnf và copy đoạn nội dung này vào dưới !includedir /etc/my.cnf.d

[mysqld]

key_buffer = 500M

table_cache = 4000

sort_buffer_size = 3M

read_buffer_size = 2M

read_rnd_buffer_size = 8M

myisam_sort_buffer_size = 64M

max_connections = 400

query_cache_type = 1

query_cache_limit = 5M

query_cache_size = 500M

tmp_table_size=20M

max_heap_table_size=20M

thread_cache_size = 64

- Khởi động lại mysql

service mysql restart

III. Cài đặt php-fpm1. Cài đặt

yum -y --enablerepo=remi install php-common php-fpm php-gd php-mysql php-pdo php-xml php-mbstring php-mcrypt php-pecl-apc php-curl php-soap

BLOG HACKING & SECURITY: QUYLEVHB.BLOGSPOT.COM Page 9

2015QUYLEVHB.BLOGSPOT.COM

Page 10: Xay Dung Vps Nginx Update

Q

Chú ý: lệnh này sẽ cài đặt php 5.4 nếu muốn cài php 5.6 các bạn chạy lệnh

yum --enablerepo=remi,remi-php56 -y install php php-common php-fpm php-mysql php-gd php-xml php-mbstring php-mcrypt php-pdo php-soap

- Khởi động php-fpm

service php-fpm start

chkconfig --levels 235 php-fpm on

BLOG HACKING & SECURITY: QUYLEVHB.BLOGSPOT.COM Page 10

2015QUYLEVHB.BLOGSPOT.COM

Page 11: Xay Dung Vps Nginx Update

Q

BLOG HACKING & SECURITY: QUYLEVHB.BLOGSPOT.COM Page 11

2015QUYLEVHB.BLOGSPOT.COM

Page 12: Xay Dung Vps Nginx Update

Q

2. Cấu hình- Mở file /etc/php-fpm.d/www.conf thay thế toàn bộ bằng

[www]

listen = 127.0.0.1:9000

listen.allowed_clients = 127.0.0.1

user = nginx

group = nginx

pm = ondemand

pm.max_children = 2

; default: min_spare_servers + (max_spare_servers - min_spare_servers) / 2

pm.start_servers = 20

pm.min_spare_servers = 1

pm.max_spare_servers = 5

pm.max_requests = 500

pm.status_path = /php_status

request_terminate_timeout = 100s

pm.process_idle_timeout = 10s;

request_slowlog_timeout = 4s

slowlog = /home/domain-cua-ban/logs/php-fpm-slow.log

rlimit_files = 131072

rlimit_core = unlimited

catch_workers_output = yes

env[HOSTNAME] = $HOSTNAME

env[PATH] = /usr/local/bin:/usr/bin:/bin

env[TMP] = /tmp

env[TMPDIR] = /tmp

env[TEMP] = /tmp

php_admin_value[error_log] = / home/domain-cua-ban/logs/php-fpm-error.log

php_admin_flag[log_errors] = on

php_value[session.save_handler] = files

php_value[session.save_path] = /var/lib/php/session

BLOG HACKING & SECURITY: QUYLEVHB.BLOGSPOT.COM Page 12

2015QUYLEVHB.BLOGSPOT.COM

Page 13: Xay Dung Vps Nginx Update

Q

Thay thế: / home/domain-cua-ban/logs bằng path của các bạn

- Truy cập /etc tải file sau về và chép đè vào

https://drive.google.com/file/d/0B5GXJ8MAIvQqZEdGVWZYQTBKOXM/view?usp=sharing

- Chạy các lệnh sau:

mkdir –p nginx:nginx /var/lib/php/session

chown -R nginx:nginx /var/lib/php

service php-fpm restart

IV. Cài đặt phpmyadmin

1. Cài đặt

yum --enablerepo=remi -y install phpMyAdmin

2. Cấu hình

mở file vhost (etc/nginx/conf.d/abc.conf)

abc.conf: Tên file vhost của bạn

Thêm vào cuối file đoạn rule sau

server {

listen 1109; # listen port

server_name localhost;

location / {

root /usr/share/phpMyAdmin; # Document root

index index.php index.html index.htm;

}

location ~ \.php$ {

root /usr/share/phpMyAdmin; # Document root

fastcgi_pass 127.0.0.1:9000;

BLOG HACKING & SECURITY: QUYLEVHB.BLOGSPOT.COM Page 13

2015QUYLEVHB.BLOGSPOT.COM

Page 14: Xay Dung Vps Nginx Update

Q

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

include fastcgi_params;

}

}

Trong đó

- 1109: Là port dể vào phpmyadmin. Các bạn có thể đặt tùy ý. Lưu ý port cần phải free và không có service nào sử dụng để tránh xung đột

Danh sách port các bạn có thể xem ở đây: http://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers

4. Mở port

Mở file /etc/sysconfig/iptables thêm vào rule sau

-A INPUT -m state --state NEW -p tcp --dport 1109 -j ACCEPT

- Khởi động lại iptables

service iptables restart

- Khởi động lại nginx

service nginx restart

- Truy cập theo địa chỉ

http://domain.com:1109

hoặc

http://ip-server:2313

BLOG HACKING & SECURITY: QUYLEVHB.BLOGSPOT.COM Page 14

2015QUYLEVHB.BLOGSPOT.COM

Page 15: Xay Dung Vps Nginx Update

Q

V. Cài đặt vsftpd1. Cài đặt

yum -y install vsftpd

2. Cấu hìnhMở file /etc/vsftpd/vsftpd.conf

- Tìmanonymous_enable=YES

sửa thànhanonymous_enable=NO

- Tìm#ascii_upload_enable=YES#ascii_download_enable=YES

sửa thànhascii_upload_enable=YESascii_download_enable=YES

- Tìm#ls_recurse_enable=YES

sửa thànhls_recurse_enable=YES

- Tìm#chroot_local_user=YES#chroot_list_enable=YES#chroot_list_file=/etc/vsftpd/chroot_list

sửa thànhchroot_local_user=YESchroot_list_enable=YES

chroot_list_file=/etc/vsftpd/chroot_list

- Thêm 2 dòng sau xuống cuối cùng

BLOG HACKING & SECURITY: QUYLEVHB.BLOGSPOT.COM Page 15

2015QUYLEVHB.BLOGSPOT.COM

Page 16: Xay Dung Vps Nginx Update

Q

local_root=public_htmluse_localtime=YES

- Tạo file chroot_list trong thư mục /etc/vsftpd- Khởi động vsftpdservice vsftpd start

- Cấu hình vsftpd tự khởi động

chkconfig --levels 235 vsftpd on

VI. Cài đặt Xcache

Chạy lần lượt các lệnh sau

yum --enablerepo=remi -y install xcache xcache-admin

Nếu dùng php 5.6 thì chạy lệnh sau

yum --enablerepo=remi,remi-php56 -y install xcache xcache-admin

service nginx restart

service php-fpm restart

VII. Cài đặt csf1. Cài các lib cần thiết

BLOG HACKING & SECURITY: QUYLEVHB.BLOGSPOT.COM Page 16

2015QUYLEVHB.BLOGSPOT.COM

Page 17: Xay Dung Vps Nginx Update

Q

Yum -y install perl-libwww-perl

2. Cài đặt csf

cd /tmp

wget http://www.configserver.com/free/csf.tgz

tar -xzf csf.tgz

cd csf

sh install.sh

3. Tiến hành test csf

perl /usr/local/csf/bin/csftest.pl

Nếu thấy báo như hình là ok

4. Mở file /etc/csf/csf.confSửa: TESTING = "1"Thành: TESTING = "0"

BLOG HACKING & SECURITY: QUYLEVHB.BLOGSPOT.COM Page 17

2015QUYLEVHB.BLOGSPOT.COM

Page 18: Xay Dung Vps Nginx Update

Q

5. Run các lệnh sau để khởi động csfchkconfig --level 235 csf onservice csf restart

VIII. Bảo mật cơ bản cho nginx1. Chmod- Chmod thư mục: home, domain-của-bạn, public_html, etc/nginx,

etc/nginx/conf.d, etc/php-fpm.d về 711- Chmod tất cả các file trong thư mục /etc/nginx, /etc/php-fpm.d,

/etc/phpMyAdmin và /etc/nginx/conf.d về 600- Chmod file /etc/phpMyAdmin/ config.inc.php và /etc/php.ini, passwd,

passwd- về 600

2. Chống run shell thư mục uploads- Add rule chống run shell trong thư mục upload

Mở file vhost thêm vào

location /uploads/ {location ~ .*\.(inc|sql|php|cgi|pl|jsp|txt|php5|shtml|html|php4|phtml|nok|love|kid|rp)?${deny all;}}

- Thay /uploads/ bằng path thư mục upload của các bạn

3. Đặt pass 2 cho admincpThêm rule sau vào file vhost

location /admincp { auth_basic "Private"; auth_basic_user_file /etc/nginx/conf/.htpasswd; }

- Thay /admincp bằng path thư mục admincp của bạn- Tạo file .htpasswd trong thư mục /etc/nginx. Để tạo các bạn dung lệnh

sau

htpasswd -bc /etc/nginx/.htpasswd myusername mypassword

Ví dụ:

BLOG HACKING & SECURITY: QUYLEVHB.BLOGSPOT.COM Page 18

2015QUYLEVHB.BLOGSPOT.COM

Page 19: Xay Dung Vps Nginx Update

Q

htpasswd -bc /etc/nginx/.htpasswd quylevhb 123456

IX. Tài liệu

Các bạn có thể xem them tại:

http://quylevhb.blogspot.com/search/label/Nginx

BLOG HACKING & SECURITY: QUYLEVHB.BLOGSPOT.COM Page 19

2015QUYLEVHB.BLOGSPOT.COM