1. Trang chủ
  2. » Công Nghệ Thông Tin

xây dựng vpsm nginx

18 231 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 18
Dung lượng 345,71 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

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... Tạo virtual host v

Trang 1

CÀI ĐẶT VPS

- 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

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

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

Trang 2

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

I. 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

Trang 3

chmod +x /etc/init.d/nginx

- Run tiếp các lệnh sau

mkdir -p /var/cache/ngx_pagespeed

mkdir -p /var/log/nginx

chown -R nginx:nginx /var/cache/ngx_pagespeed

chown -R nginx:nginx /var/log/nginx

- Khởi động nginx

service 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_html

mkdir /home/domain-của-bạn/logs

chmod 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

Trang 4

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_pagespeed

location ~ \.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

Trang 5

II. Cài đặt mariadb

1. Add repo

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

[mariadb]

name = MariaDB

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

gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB

gpgcheck=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

Trang 7

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

Chạy lệnh:

service mysql start

chkconfig mysql on

mysql_secure_installation

Trang 8

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 current

password for the root user If you've just installed MariaDB, and

you 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 MariaDB

root user without the proper authorisation.

Set root password? [Y/n] Y

New 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 anyone

to log into MariaDB without having to have a user account created for

them This is intended only for testing, and to make the installation

go a bit smoother You should remove them before moving into a

production environment.

Remove anonymous users? [Y/n] Y

Success!

Normally, root should only be allowed to connect from 'localhost' This

ensures 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 can

access This is also intended only for testing, and should be removed

before 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 far

will 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 MariaDB

installation should now be secure.

Thanks for using MariaDB!

Trang 9

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-fpm

1. 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

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

Trang 10

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

Trang 12

- 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

Trang 13

Thay thế: / home/domain-cua-ban/logsbằ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

Trang 14

fastcgi_pass 127.0.0.1:9000;

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

Trang 15

V. Cài đặt vsftpd

1. Cài đặt

yum -y install vsftpd

2. Cấu hình

Mở file /etc/vsftpd/vsftpd.conf

- Tìm

anonymous_enable=YES

sửa thành

anonymous_enable=NO

- Tìm

#ascii_upload_enable=YES

#ascii_download_enable=YES

sửa thành

ascii_upload_enable=YES

ascii_download_enable=YES

- Tìm

#ls_recurse_enable=YES

sửa thành

ls_recurse_enable=YES

- Tìm

#chroot_local_user=YES

#chroot_list_enable=YES

#chroot_list_file=/etc/vsftpd/chroot_list

sửa thành

chroot_local_user=YES

chroot_list_enable=YES

chroot_list_file=/etc/vsftpd/chroot_list

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

Trang 16

- Tạo file chroot_list trong thư mục /etc/vsftpd

- Khởi động vsftpd

service 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 csf

1. Cài các lib cần thiết

Yum -y install perl-libwww-perl

Trang 17

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.conf

Sửa: TESTING = "1"

Thành: TESTING = "0"

5. Run các lệnh sau để khởi động csf

chkconfig level 235 csf on

service csf restart

VIII. Bảo mật cơ bản cho nginx

Trang 18

1. 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 admincp

Thê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ụ:

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

Ngày đăng: 04/04/2015, 18:40

w