1. Cài đặt Apache, PHP, MySQL.
Các bạn vui lòng xem cách cài đặt Apache, PHP và MySQl ở các bài viết trước nhé.
Cài đặt Apache
Cài đặt MySQL
2. Cài đặt NGINX.
Tạo 1 file tên là nginx.repo trong thư mục /etc/yum.repos.d/ rồi copy đoạn dưới đây vào:
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
Cài đặt NGiNX bằng lệnh sau:
yum install nginx
3. Cấu hình NGINX và Apache làm reverse proxy.
a. Cấu hình Apache làm reverse proxy listen port 8081
Tiến hành sửa file /etc/httpd/conf/httpd.conf như sau:
Listen 80
# Sửa thành
Listen 8081
DirectoryIndex index.html index.html.var
# Sửa thành
DirectoryIndex index.php index.html
#NameVirtualHost *:80
# Bỏ dấu # và thay thành port 8081
NameVirtualHost *:8081
Lưu file này lại, restart apache. Sau đó thêm 1 rule cho Iptables allow port 8081
iptables -I INPUT 1 -p tcp --dport 8081 -j ACCEPT
/etc/init.d/iptables save
/etc/init.d/iptables restart
b. Cấu hình NGINX
Tiến hành cấu hình NGINX như sau:
Backup file /etc/nginx/nginx.conf thành /etc/nginx/nginx.conf.bak
mv /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
Sau đó tiến hành sửa nội dung file /etc/nginx/nginx.conf với nội dung bên dưới
vi /etc/nginx/nginx.conf
worker_processes 8;
pid /var/run/nginx.pid;
events {
worker_connections 10240;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
tcp_nopush on;
tcp_nodelay on;
sendfile on;
log_format bytes '$bytes_sent $request_length';
keepalive_timeout 2;
types_hash_max_size 2048;
disable_symlinks if_not_owner from=$document_root;
server_tokens off;
client_max_body_size 1024m;
client_body_buffer_size 128k;
server_names_hash_bucket_size 128;
server_names_hash_max_size 10240;
gzip on;
gzip_static on;
gzip_disable "msie6";
gzip_http_version 1.1;
gzip_vary on;
gzip_comp_level 6;
gzip_proxied any;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript text/x-js;
gzip_buffers 16 8k;
proxy_connect_timeout 150s;
proxy_send_timeout 150s;
proxy_read_timeout 150s;
proxy_buffer_size 128k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
include /etc/nginx/conf.d/*.conf;
}
Restart lại NGINX bằng lệnh:
/etc/init.d/nginx restart
4. Thêm Virtual host cho NGINX và Apache.
Apache
Tạo file abc.conf trong thư mục /etc/httpd/conf.d với abc là tên domain virtual host, với nội dung như sau:
vi /etc/httpd/conf.d/abc.conf
<VirtualHost *:8081>
ServerAdmin admin@abc.com
DocumentRoot "/home/web/"
ServerName abc.com
ErrorLog "logs/abc-error.log"
CustomLog "logs/abc-access.log" common
<Directory "/home/web/">
Order Deny,Allow
Allow from all
Options Indexes FollowSymLinks MultiViews
AllowOverride all
</Directory>
</VirtualHost>
Restart lại apache
/etc/init.d/httpd restart
NGINX
Tạo file abc.conf trong thư mục /etc/nginx/conf.d với abc là tên domain virtual host, với nội dung như sau:
vi /etc/nginx/conf.d/abc.conf
server
{
listen IP:80;
server_name abc.com www.abc.com;
access_log /var/log/abc.com.log;
error_log /var/log/abc.error.log;
root /home/web/;
index index.php index.html index.htm;
location /
{
# access_log off;
proxy_buffering off;
# proxy_redirect http://www.abc.com:8081 http://abc.com;
proxy_pass http://IP:8081;
proxy_set_header X-Client-IP $remote_addr;
proxy_set_header X-Accel-Internal /nginx_static_files;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /nginx_static_files/
{
# access_log /var/log/nginx/access_log_proxy;
alias /home/web/;
internal;
}
}
Thay thế IP bằng địa chỉ IP của server, sau đó restart lại NGINX
/etc/init.d/nginx restart