คำสั่งที่ใช้ในการติดตั้ง
1 2 3 4 5 | sudo apt install apache2 # สั่งให้เริ่มทำงาน และตั้งค่าให้เริ่มทำงานเมื่อเปิดเครื่อง sudo systemctl start apache2 sudo systemctl enable apache2 |
คำสั่งจัดการ Service ต่างๆ
1 | sudo systemctl [status| enable |disable|start|restart|stop] apache2 |
การตั้งค่า Virtual Host
ใช้กรณีที่เครื่องเราทำหน้าที่เป็น Host ให้กับเว็บมากกว่าหนึ่งขึ้นไป เช่น www.example.com และ www.example.netเพื่อบอกให้ Apache 2 รู้ว่าถ้ามีคนเข้ามาดู www.example.com จะใช้ข้อมูลชุดไหน และ www.example.net ใช้ข้อมูลชุดไหน
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | <VirtualHost *:80> ServerAdmin mail@example.com ServerName example.com ServerAlias www.example.com DocumentRoot /var/www/html/example .com ErrorLog /path/to/your_error .log CustomLog /path/to/your_custom .log combined < /VirtualHost > <VirtualHost *:80> ServerAdmin mail@example.net ServerName example.net ServerAlias www.example.net DocumentRoot /var/www/html/example .net # ถ้าต้องการใช้กับ Reverse Proxy # ProxyPreserveHost On # ProxyPass / http://127.0.0.1:8080/ # ProxyPassReverse / http://127.0.0.1:8080/ ErrorLog /path/to/your_error .log CustomLog /path/to/your_custom .log combined < /VirtualHost > # ServerAdmin อีเมล์ของผู้ดูแล # ServerName บอกถึงว่าตัว Server เอง ส่วน ServerAlias คือชื่อ URL ที่จะต้องตรงกัน # จะใช้แค่ ServerName อย่างเดียวก็ได้ ต้องเขียนให้ครบ เช่น www.example.com ไปเลย |
1 2 3 4 5 6 7 8 9 | <VirtualHost *:80> # Visual Host และ Redirect ServerName example.net ServerAlias www.example.net Redirect / https: //www .example.net/ < /VirtualHost > |
ใครส่งสัยไปอ่านที่ mod_core หรือตัวอย่างอื่นๆ 2.4/vhost/example
การเปิด SSL ให้กับ Apache 2
1 2 | sudo a2enmod ssl sudo a2ensite default-ssl |
1 2 3 4 5 6 7 8 9 10 | sudo nano /etc/apache2/sites-enabled/default-ssl .conf <VirtualHost *:443> SSLEngine on SSLCertificateFile /path/to/your_certification .crt SSLCertificateKeyFile /path/to/your_private .key # ไฟล์นี้ไม่มีก็ได้ SSLCertificateChainFile /path/to/your_chain .crt < /VirtualHost > |
โฟลเดอร์ไฟล์คอนฟิกไฟล์ (config) จะอยู่ที่ /etc/apache2/sites-available/
และ ถ้าใช้คำสั่ง a2ensite
ไฟล์ต้นฉบับอยู่ที่ sites-avaliable ส่วนลิงค์จะอยู่ที่ sites-enabled เพื่อให้ง่ายต่อการใช้ ถ้าไม่ใช่ก็แค่ลบใน sites-enabled ออก
การเปิด Mod Rewrite
เปิดการใช้งาน Mod Rewrite ของ Apache21 2 | sudo a2enmod rewrite sudo systemctl restart apache2 |
1 2 3 4 5 | < Directory /var/www/html> Options Indexes FollowSymLinks MultiViews AllowOverride All Require all granted </ Directory > |
1 | sudo systemctl restart apache2 |
การทำ Reverse Proxy และ Load Balance
ติดตั้ง Module ที่จำเป็นตามนี้mod_proxy เป็นโมดูลที่ทำให้ Apache 2 ทำการส่งต่อ และทำหน้าที่เป็น Gateway
mod_proxy_http รองรับการใช้ Proxy ผ่านทาง HTTP
mod_proxy_connect รองรับการใช้ Proxy ผ่านทาง HTTPS ด้วย
mod_proxy_balancer และ mod_lbmethod_byrequests เพิ่มความสามารถในเพื่อทำ Load Balance
1 2 3 4 5 6 7 | sudo a2enmod proxy sudo a2enmod proxy_http sudo a2enmod proxy_connect sudo a2enmod proxy_balancer sudo a2enmod lbmethod_byrequests sudo systemctl restart apache2 |
การ Config เพื่อทำ Reverse Proxy HTTP -> HTTP ตามนี้
1 2 3 4 5 6 7 8 9 10 11 12 13 | <VirtualHost *:80> ProxyPreserveHost On ProxyPass / http: //192 .168.0.5:80/ ProxyPassReverse / http: //192 .168.0.5:80/ # ถ้า Server ปลายทางมี path ควรทำให้เหมือนกัน ProxyPass /example/ http: //192 .168.0.5:80 /example/ ProxyPassReverse /example/ http: //192 .168.0.5:80 /example/ < /VirtualHost > sudo systemctl restart apache2 |
การ Config เพื่อทำ Reverse Proxy HTTPS -> HTTPS ตามนี้
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <VirtualHost *:443> SSLProxyEngine On ProxyPreserveHost On ProxyPass / https: //172 .16.0.5:443/ ProxyPassReverse / https: //172 .16.0.5:443/ # ถ้า Server ปลายทางมี path ควรทำให้เหมือนกัน ProxyPass /example/ https: //172 .16.0.5:443 /example/ ProxyPassReverse /example/ https: //172 .16.0.5:443 /example/ < /VirtualHost > sudo systemctl restart apache2 |
การ Config เพื่อทำ Load Balance ตามนี้
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <VirtualHost *:80> <Proxy balance: //mycluster > BalancerMember http: //127 .0.0.1:8080 BalancerMember http: //127 .0.0.1:8082 < /Proxy > ProxyPreserveHost On ProxyPass / banalcer: //mycluster/ ProxyPassReverse / balancer: //mycluster/ < /VirtualHost > sudo systemctl restart apache2 |
ขอบคุณข้อมูล
Digicert | Ubuntu Server Apache2 OpenSSL
Digital Ocean | Virtual Host, Proxy Server and Load Balance
Stack Overflow
Proxy with Authentication, Proxy with HTTPS
ไม่มีความคิดเห็น:
แสดงความคิดเห็น