UnixLinuxVN – Bài thực hành linux LPIC02 Linux Basic Course 1 Bài thực hành Web, Squid, php và Mysql Cài đặt web server apache 1 Cấu hình card mạng máy server và client ở chế độ host only và các địa c.
Trang 1Bài thực hành
Web, Squid, php và Mysql
Cài đặt web server apache
1 Cấu hình card mạng máy server và client ở chế độ host-only và các địa chỉ sau (chú ý tên card
mạng thay đổi tùy hệ thống – giả thiết card mạng đang dùng là ens33):
[root@server etc] #ifconfig
[root@server etc] #ifconfig ens33 10.0.0.1/24 up
[root@client etc] #ifconfig ens33 10.0.0.2/24 up
2 Thử kết nối
[root@client etc] # ping 10.0.0.1
3 Cài đặt dịch vụ web server
[root@server etc] #yum -y install httpd
4 Chạy dịch vụ httpd
[root@server etc] #service httpd start
Hoặc [root@server etc] #systemctl restart httpd
5 Kiểm tra cổng đã mở server
[root@server etc] # netstat –an | grep 80
6 Vào máy client thử nghiệm trang web
Cấu hình web server sử dụng IP based virtual host
1 Cấu hình httpd như sau:
[root@server etc] # vi /etc/httpd/httpd.cond
<VirtualHost 10.0.0.1>
DocumentRoot /var/www/html/virtualhost1/
</VirtualHost>
<VirtualHost 10.0.0.2>
DocumentRoot /var/www/html/virtualhost2/
</VirtualHost>
2 Tạo trang web index.html trong thư mục /var/html/www/virtualhost1
[root@server etc] # vi /var/html/www/virtualhost1/index.html
<html>
<body>
This is virtual host 1
</body>
</html >
3 Tạo trang web index.html trong thư mục /var/html/www/virtualhost2
[root@server etc] # vi /var/html/www/virtualhost2/index.html
<html>
<body>
This is virtualhost 2
</body>
</html >
4 Chạy dịch vụ httpd
[root@server etc] #service httpd start
5 Kiểm tra cổng đã mở server
[root@server etc] # netstat –an | grep 80
Trang 26 Vào máy client thử nghiệm trang web
[root@client etc] #mozilla http://10.0.0.1 &
7 Vào máy test thử nghiệm trang web
[root@test etc] #mozilla http://10.0.0.2 &
Cấu hình web server sử dụng Name based virtual host
1 Cấu hình DNS trên máy server để phân dải hai tên miền sau: (hoặc dùng file host trên
window)
virtualhost1.lablinux.com IN A 10.0.0.1
virtualhost2.lablinux.com IN A 10.0.0.1
2 Cấu hình card mạng máy client và máy server chỉ tới DNS của máy server
[root@server etc] #vi /etc/resolv.conf
Nameserver 10.0.0.1
[root@client etc] #vi /etc/resolv.conf
Nameserver 10.0.0.1
3 Thử kết nối
[root@test etc] # ping virtualhost1.lablinux.com
[root@test etc] # ping virtualhost2.lablinux.com
4 Cấu hình httpd như sau:
[root@server etc] # vi /etc/httpd/conf.d/virtualhost.conf
NameVirtualHost 10.0.0.1 #Dòng này chỉ có với CentOS, Redhat 6 trở xuống
<VirtualHost 10.0.0.1>
ServerAdmin webmaster@lablinux.vn
DocumentRoot /var/www/html/virtualhost1
ServerName virtualhost1.lablinux.com
</VirtualHost>
<VirtualHost 10.0.0.1>
ServerAdmin webmaster@lablinux.vn
DocumentRoot /var/www/html/virtualhost1
ServerName virtualhost2.lablinux.com
</VirtualHost>
5 Chạy dịch vụ httpd
[root@server etc] #service httpd start
[root@server etc] #systemctl restart httpd
6 Kiểm tra cổng đã mở server
[root@server etc] # netstat –an | grep 80
7 Vào máy client thử nghiệm trang web
[root@client etc] #mozilla http://virtualhost1.lablinux.com &
8 Vào máy test thử nghiệm trang web
[root@test etc] #mozilla http://virtualhost2.lablinux.com &
Cấu hình webserver có vùng bị giới hạn truy cập
Trong virtualhost1 có vị trí /var/www/html/virtualhost1/safe cần giới hạn truy cập
1 Cấu hình httpd của máy server như sau:
[root@server etc] # vi /etc/httpd/conf.d/virtualhost.conf
Khai báo đoạn cấu hình sau trong phần virtualhost1
Trang 3<Directory /var/www/html/virtualhost1/safe>
Order allow,deny
Deny from 10.0.0.2
Allow from all
</Directory>
Alias /safe /var/www/safe
2 Tạo trang web vào thư mục /var/www/html/virtualhost1/safe
[root@server etc] # mkdir /var/www/html/virtualhost1/safe
[root@server safe] # vi index.html
This is safe page
3 Chạy lại dịch vụ httpd
[root@server etc] #service httpd restart
4 Vào máy client thử nghiệm trang web
[root@client etc] #mozilla http://virtualhost1.lablinux.com/safe &
5 Vào máy test thử nghiệm trang web
[root@test etc] #mozilla http://virtualhost1.lablinux.com/safe &
Cấu hình webserver để đòi hỏi chứng thực
1 Cấu hình lại httpd của máy server như sau:
[root@server etc] # vi /etc/httpd/conf.d/virtualhost.conf
Khai báo đoạn cấu hình sau trong phần virtualhost1
<Directory /var/www/safe>
AuthType basic
AuthName "protected site"
AuthUserFile /etc/httpd/conf/safe.passwd
Require user ipmac
</Directory>
Alias /safe /var/www/safe
2 Tạo file safe.passwd để lưu mật mã của user ipmac
[root@server etc] # htpasswd -c /etc/httpd/conf/safe.passwd ipmac
3 Chạy dịch vụ httpd
[root@server etc] #service httpd restart
Hoặc
[root@server etc] #systemctl restart httpd
4 Vào máy client thử nghiệm trang web
[root@client etc] #mozilla http://virtualhost1.lablinux.com/safe &
Tạo trang web với php
1 Cài đặt gói php
[root@server etc]# yum -y install php
2 Tạo file index.php trong thư mục virtualhost1 như sau:
<HTML>
<HEAD><TITLE>PHP Test </TITLE></HEAD>
<BODY>
<?echo “this is a test” ?>
</BODY>
</HTML>
3 Chạy lại dịch vụ httpd
[root@server etc] #service httpd restart
Trang 44 Vào máy client thử nghiệm trang web
[root@client etc] #mozilla http://10.0.0.1/index.php
Tạo trang web https với apache
1 Cài đặt module ssl hỗ trợ tính năng mã hóa cho web server apache
[root@server etc]# yum -y install mod_ssl
2 Kiểm tra nội dung khai báo mặc định cho https
[root@server etc]# more /etc/httpd/conf.d/ssl.conf
3 Khai báo cấu hình virtual host mới cho https
[root@server etc]# vi /etc/httpd/conf.d/virtualhost.conf
NameVirtualHost 10.0.0.1:443 #không cần khai báo dòng này với bản Enterprise linux 7 trở lên
<VirtualHost 10.0.0.1:443>
ServerName virtualhost1.lablinux.com
DocumentRoot /var/www/html/virtualhost1
SSLEngine on
SSLCertificateFile /etc/pki/tls/certs/localhost.crt
SSLCertificateKeyFile /etc/pki/tls/private/localhost.key
</VirtualHost>
4 Chạy lại dịch vụ httpd
[root@server etc] #service httpd restart
Hoặc
[root@server etc] #systemctl restart httpd
5 Vào máy client thử nghiệm trang web
[root@client etc] #mozilla https://virtualhost1.lablinux.com
Cấu hình Nginx webserver
1 Cài đặt Nginx webserver
[root@server etc] #yum install epel-release
[root@server etc] #yum -y install nginx
2 Bật dịch vụ nginx
[root@server etc] #systemctl enable nginx
[root@server etc] #systemctl disable httpd
[root@server etc] #systemctl stop httpd
[root@server etc] #systemctl start nginx
Cấu hình virtualhost với nginx
1 Tạo file cấu hình virtualhost
[root@server etc] #vi /etc/nginx/conf.d/virtualhost.conf
server {
listen 80;
server_name web1.com www.web1.com;
location / {
root /var/www/html/virtualhost1;
}
}
server {
listen 80;
server_name web2.com www.web2.com;
location / {
Trang 5root /var/www/html/virtualhost2;
}
}
2 Khởi động lại dịch vụ nginx
[root@server etc] #systemctl restart nginx
3 Vào máy client thử nghiệm trang web
[root@client etc] #mozilla http:// web1.com/ &
4 Vào máy test thử nghiệm trang web
[root@test etc] #mozilla http:// web2.com / &
Trang 6Cài đặt proxy Squid
1 Cài đặt gói squid proxy
[root@server etc] #yum -y install squid
2 Enable squid proxy
[root@server etc] #systemctl enable squid
[root@server etc] #systemctl start squid
3 Kiểm tra squid proxy
[root@server etc] #curl -x http://10.0.0.1:3128 -L http://google.com
Cấu hình xác thực cho squid proxy
1 Chỉnh sửa file cấu hình proxy như sau:
[root@server etc]# vi /etc/squid/squid.conf
auth_param basic program /usr/lib64/squid/basic_ncsa_auth /etc/squid/passwd
auth_param basic children 5
auth_param basic realm Squid Basic Authentication
auth_param basic credentialsttl 2 hours
acl auth_users proxy_auth REQUIRED
http_access allow auth_users
2 Khởi động lại dịch vụ squid
[root@server etc]# systemctl restart squid
3 Truy cập thử từ client sẽ hỏi thông tin đăng nhập
Cấu hình giới hạn website
1 Tạo file lưu các trang web giới hạn như sau:
[root@server etc] #vi /etc/squid/blocked_sites
facebook.com
vnexpress.net
dantri.com.vn
docbao.com.vn
unixlinuxvn.com
2 Khai báo giới hạn trong squid:
[root@server etc] #vi /etc/squid/squid.conf
acl blocked_sites dstdomain "/etc/squid/blocked_sites"
http_access deny blocked_sites
3 Khởi động lại squid proxy:
[root@server etc]# systemctl restart squid
4 Từ client truy cập thử các site giới hạn sẽ nhận được thông báo tương tự như sau:
Customize err page: /usr/share/squid/errors
Trang 7Cấu hình reverse proxy với Nginx webserver
1 Tạo cấu hình virtualhost cho web3
[root@server etc] #vi /etc/nginx/conf.d/virtualhost.conf
server {
listen 80;
server_name web3.com www.web3.com;
location / {
proxy_pass http://10.0.0.3
}
}
2 Khởi động lại dịch vụ nginx
[root@server etc] #systemctl restart nginx
1 Vào máy client thử nghiệm trang web
[root@client etc] #mozilla http:// web3.com/ &
Cấu hình reverse proxy với apache webserver
1 Tạo cấu hình virtualhost cho web3
[root@server etc] #vi /etc/httpd/conf.d/virtualhost.conf
<VirtualHost *:80>
ServerName web3.com
ServerAlias www.web3.com
ProxyPass "/" "http://10.0.0.3" timeout=120
</VirtualHost>
2 Khởi động lại dịch vụ httpd
[root@server etc] #systemctl restart httpd
2 Vào máy client thử nghiệm trang web
[root@client etc] #mozilla http:// web3.com/ &
Cấu hình reverse proxy load balancing với apache webserver
1 Tạo cấu hình virtualhost cho web3
[root@server etc] #vi /etc/httpd/conf.d/virtualhost.conf
<VirtualHost *:80>
ServerName web3.com
ServerAlias www.web3.com
<Proxy "balancer://myload">
BalancerMember http://10.0.0.4:80
BalancerMember "http://10.0.0.4:80"
</Proxy>
ProxyPass "/" "balancer://myload" timeout=120
</VirtualHost>
2 Khởi động lại dịch vụ httpd
[root@server etc] #systemctl restart httpd
3 Vào máy client thử nghiệm trang web
[root@client etc] #mozilla http:// web3.com/ &
Cấu hình reverse proxy load balancing với nginx webserver
1 Tạo cấu hình virtualhost cho web3
[root@server etc] #vi /etc/nginx/conf.d/virtualhost.conf
Trang 8server {
listen 80;
server_name web3.com www.web3.com;
upstream backend {
server 10.0.0.3 weight=5;
server 10.0.0.4;
}
location / {
proxy_pass http://backend
}
}
2 Khởi động lại dịch vụ nginx
[root@server etc] #systemctl restart nginx
3 Vào máy client thử nghiệm trang web
[root@client etc] #mozilla http:// web3.com/ &