ค้นหาเว็บไซต์

วิธีติดตั้ง Lighttpd ด้วย PHP และ MariaDB บน CentOS/RHEL 8/7


Lighttpd เป็นเว็บเซิร์ฟเวอร์แบบโอเพ่นซอร์ส ปลอดภัย รวดเร็ว ยืดหยุ่น และมีประสิทธิภาพมากกว่า ออกแบบมาสำหรับสภาพแวดล้อมที่เน้นความเร็วโดยใช้หน่วยความจำน้อยกว่าเมื่อเทียบกับเว็บเซิร์ฟเวอร์อื่นๆ

สามารถรองรับการเชื่อมต่อแบบขนานได้มากถึง 10,000 ในเซิร์ฟเวอร์เดียวพร้อมการจัดการโหลด CPU ที่มีประสิทธิภาพ และมาพร้อมกับชุดคุณลักษณะขั้นสูง เช่น FastCGI, SCGI, Auth, การบีบอัดเอาต์พุต, การเขียน URL ใหม่ และอื่นๆ อีกมากมาย

Lighttpd เป็นโซลูชันที่ยอดเยี่ยมสำหรับเซิร์ฟเวอร์ Linux ทุกเครื่อง เนื่องจากมีโครงสร้างพื้นฐาน io ความเร็วสูงที่ช่วยให้เราสามารถปรับขนาดประสิทธิภาพที่ดีขึ้นหลายเท่าด้วยฮาร์ดแวร์เดียวกันมากกว่าเว็บเซิร์ฟเวอร์ทางเลือกอื่นๆ

ในบทช่วยสอนนี้ เราจะอธิบายวิธีการติดตั้ง Lighttpd ด้วย PHP, PHP-FPM และ MariaDB บน CentOS/RHEL 8/7 การกระจายด้วยชื่อโฮสต์ linux-console.net และที่อยู่ IP 192.168.0.103

ขั้นตอนที่ 1: ติดตั้งเว็บเซิร์ฟเวอร์ Lighttpd

1. หากต้องการติดตั้ง Lighttpd ขั้นแรก คุณต้องอัปเดตแพ็คเกจซอฟต์แวร์ระบบและที่เก็บข้อมูลที่มีอยู่โดยใช้คำสั่ง yum ต่อไปนี้

yum -y update

2. ถัดไป คุณต้องเปิดใช้งานพื้นที่เก็บข้อมูล EPEL บนระบบของคุณและอัปเดตแพ็คเกจซอฟต์แวร์โดยใช้คำสั่งต่อไปนี้

yum -y install epel-release
yum -y update

3. เมื่อคุณเปิดใช้งานพื้นที่เก็บข้อมูล EPEL แล้ว ตอนนี้คุณสามารถติดตั้ง Lighttpd ได้โดยการเรียกใช้คำสั่งต่อไปนี้

yum install lighttpd

4. เมื่อติดตั้งแพ็คเกจ Lighttpd แล้ว คุณสามารถเริ่มต้นและเปิดใช้งานบริการเพื่อเริ่มต้นโดยอัตโนมัติในเวลาบูตและตรวจสอบให้แน่ใจว่าได้ตรวจสอบสถานะโดยใช้คำสั่งต่อไปนี้

systemctl start lighttpd
systemctl enable lighttpd
systemctl status lighttpd

5. ตอนนี้ให้ตรวจสอบเวอร์ชันของ Lighttpd ที่ติดตั้งบนระบบของคุณโดยใช้คำสั่งต่อไปนี้

lighttpd -v

lighttpd/1.4.55 (ssl) - a light and fast webserver

6. ตอนนี้ คุณต้องอนุญาตการรับส่งข้อมูล HTTP และ HTTPS บนไฟร์วอลล์ของคุณ

firewall-cmd --permanent --zone=public --add-service=http
firewall-cmd --permanent --zone=public --add-service=https
firewall-cmd --reload

7. ตอนนี้ให้เปิดเบราว์เซอร์ของคุณและชี้ไปที่ URL ต่อไปนี้เพื่อดูว่าเว็บเซิร์ฟเวอร์ Lighttpd ของคุณทำงาน

http://Your-Domain.com
OR
http://Your-IP-addr

ไดเรกทอรีรากเริ่มต้นของเอกสารสำหรับ Lighttpd คือ /var/www/lighttpd/ และไฟล์การกำหนดค่าหลักอยู่ภายใต้ /etc/lighttpd/lighttpd.conf

ขั้นตอนที่ 2: การติดตั้ง MariaDB เป็น MySQL ใน CentOS 7

8. ถัดไป ติดตั้งการสนับสนุน MySQL สำหรับ Lighttpd โดยใช้คำสั่งต่อไปนี้

yum -y install mariadb mariadb-server

9. เมื่อการติดตั้งเสร็จสมบูรณ์ ให้เริ่มต้น เปิดใช้งาน และตรวจสอบสถานะของ MariaDB โดยใช้คำสั่งต่อไปนี้

systemctl start mariadb.service
systemctl enable mariadb.service
systemctl status mariadb.service

10. สุดท้ายนี้ คุณจะต้องรักษาความปลอดภัยการติดตั้ง MariaDB ของคุณโดยออกคำสั่งต่อไปนี้

mysql_secure_installation

คุณจะได้รับคำถามสองสามข้อเกี่ยวกับการติดตั้ง MariaDB ของคุณและวิธีที่คุณต้องการรักษาความปลอดภัย คุณสามารถเปลี่ยนรหัสผ่านผู้ใช้รูทฐานข้อมูล ปิดการใช้งานฐานข้อมูลทดสอบ ปิดการใช้งานผู้ใช้ที่ไม่ระบุชื่อ และปิดการใช้งานการเข้าสู่ระบบรูทจากระยะไกล

ผลลัพธ์ตัวอย่าง
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): Enter OK, successfully used password, moving on... Setting the root password ensures that nobody can log into the MariaDB root user without the proper authorization. Set root password? [Y/n] y New password: Re-enter new password: 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!

11. ลองเชื่อมต่อกับเซิร์ฟเวอร์ MySQL และตรวจสอบฐานข้อมูลที่มีอยู่ในเซิร์ฟเวอร์ฐานข้อมูลของคุณด้วยคำสั่งต่อไปนี้บนเทอร์มินัล

mysql -u root -p
ผลลัพธ์ตัวอย่าง
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 5.5.60-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)

MariaDB [(none)]>

ขั้นตอนที่ 3: การติดตั้ง PHP และ PHP-FPM ด้วย FastCGI บน CentOS 7

12. หากต้องการเปิดใช้งาน PHP-FPM ด้วยการรองรับ FastCGI ขั้นแรกคุณต้องติดตั้ง PHP พร้อมกับส่วนขยายที่จำเป็น โดยใช้คำสั่งต่อไปนี้

yum -y install php php-mysqlnd php-pdo php-gd php-mbstring

13. เมื่อติดตั้ง PHP แล้ว ให้เปิดใช้งานการสนับสนุน PHP-FPM และ FastCGI สำหรับ Lighttpd > ในการดำเนินการนี้ คุณจะต้องติดตั้งแพ็คเกจเหล่านี้ด้วย

yum -y install php-fpm lighttpd-fastcgi

14. ตอนนี้ให้เปิดไฟล์ชื่อ /etc/php-fpm.d/www.conf

vi /etc/php-fpm.d/www.conf

ตั้งค่าผู้ใช้และกลุ่มเป็น Lighttpd

; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
;       will be used.
; RPM: apache Choosed to be able to access some dir as httpd
user = lighttpd
; RPM: Keep a group allowed to write in log dir.
group = lighttpd

นอกจากนี้ PHP-FPM โดยค่าเริ่มต้นจะใช้ซ็อกเก็ต /var/run/php/php7.0-fpm.sock คุณต้องสร้าง PHP-FPM เพื่อใช้การเชื่อมต่อ TCP ทำให้บรรทัด listen มีลักษณะดังนี้:

;listen = /var/run/php/php7.0-fpm.sock
listen = 127.0.0.1:9000 

15. ตอนนี้ให้เริ่มบริการ PHP-FPM และตรวจสอบให้แน่ใจว่าได้เปิดใช้งานให้เริ่มโดยอัตโนมัติในเวลาบูตโดยใช้คำสั่งต่อไปนี้

systemctl start php-fpm.service
systemctl enable php-fpm.service

ขั้นตอนที่ 4: เปิดใช้งาน PHP และ PHP-FPM ด้วย FastCGI ใน Lighttpd

16. ที่นี่ เราจำเป็นต้องแก้ไขไฟล์สามไฟล์ /etc/php.ini, /etc/lighttpd/modules.conf และ /etc/lighttpd/conf.d/fastcgi.conf เพื่อเปิดใช้งานการสนับสนุน PHP สำหรับ Lighttpd

เปิดไฟล์แรก /etc/php.ini

vi /etc/php.ini

ยกเลิกการแสดงความคิดเห็นในบรรทัดต่อไปนี้ที่ระบุว่า line cgi.fix_pathinfo=1

; cgi.fix_pathinfo provides *real* PATH_INFO/PATH_TRANSLATED support for CGI.  PHP's
; previous behaviour was to set PATH_TRANSLATED to SCRIPT_FILENAME, and to not grok
; what PATH_INFO is.  For more information on PATH_INFO, see the cgi specs.  Setting
; this to 1 will cause PHP CGI to fix its paths to conform to the spec.  A setting
; of zero causes PHP to behave as before.  Default is 1.  You should fix your scripts
; to use SCRIPT_FILENAME rather than PATH_TRANSLATED.
; http://php.net/cgi.fix-pathinfo
cgi.fix_pathinfo=1

จากนั้นเปิดไฟล์ที่สองชื่อ /etc/lighttpd/modules.conf

vi /etc/lighttpd/modules.conf

ยกเลิกการแสดงความคิดเห็นในบรรทัดต่อไปนี้ที่ระบุว่า รวม “conf.d/fastcgi.conf”

##
## FastCGI (mod_fastcgi)
##
include "conf.d/fastcgi.conf"

จากนั้น เปิดไฟล์ที่สามชื่อ /etc/lighttpd/conf.d/fastcgi.conf

vi /etc/lighttpd/conf.d/fastcgi.conf

ตอนนี้เพิ่มคอนเทนเนอร์ต่อไปนี้ที่ด้านล่างของไฟล์แล้วบันทึก

fastcgi.server += ( ".php" =>
        ((
                "host" => "127.0.0.1",
                "port" => "9000",
                "broken-scriptfilename" => "enable"
        ))
)

เริ่มบริการ Lighttpd ใหม่เพื่อให้สอดคล้องกับการเปลี่ยนแปลงและเปิดใช้งานการสนับสนุน PHP

systemctl restart lighttpd

ขั้นตอนที่ 5: ทดสอบ PHP และ PHP-FPM ด้วยการสนับสนุน FastCGI ใน Lighttpd

17. ดังที่เราได้กล่าวไว้ข้างต้นว่ารูทเอกสารเริ่มต้นสำหรับ Lighttpd คือ /var/www/lighttpd/ ดังนั้น เราจะสร้างไฟล์ phpinfo.php ลงในไดเร็กทอรีนี้

vi /var/www/lighttpd/info.php

เพิ่มบรรทัดต่อไปนี้ลงไป โค้ดเล็กๆ นี้จะแสดงข้อมูลเกี่ยวกับการติดตั้ง PHP พร้อมด้วยเวอร์ชันต่างๆ

<?php
phpinfo();
?>

18. เปิดเบราว์เซอร์ของคุณและไปที่ลิงก์ต่อไปนี้

http://Your-Domain.com/info.php
OR
http://Your-IP-addr/info.php

คุณจะเห็นข้อมูลการทำงานของ PHP, PHP-FPM, และ MySQL พร้อมโมดูลอื่นๆ อีกมากมายที่เปิดใช้งานอยู่แล้ว