ติดตั้ง Nginx-MariaDB-PHP7.x บน Debian 10

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Software ทั้ง 3 ตัวนี้ Nginx, MariaDB และ PHP เป็นที่นิยมของนักพัฒนาเวปไซต์ ด้วยความที่สะดวก คุ้นเคยสำหรับนักพัฒนาฯ แต่สำหรับ SysAdmin บางครั้งก็น่าปวดหัวด้วยความที่เป็นของใหม่บ้าง ความไม่รองรับบนระบบ

การติดตั้งระบบทั้ง 3 ตัวนี้ แนะนำให้ติดตั้งตามลำดับ Nginx → MariaDB → PHP 7 โดยมีวิธีขั้นตอนดังต่อไปนี้

  • ก่อนการติดตั้งด้วย apt ทุกครั้ง ควรจะ update package repository ก่อนเสมอ sudo apt update

  • Nginx ที่ใช้ในบทความนี้ เป็น nginx version: nginx/1.14.2 เราจะมาตั้งค่าสำหรับ PHP หลังจากได้ติดตั้ง PHP แล้ว

$ sudo apt install nginx
  • จากการตรวจสอบ package repository พบว่า MariaDB package ชื่อ mariadb-server-10.3 apt search ^mariadb
$ sudo apt install mariadb-server-10.3
  • และสุดท้ายติดตั้ง PHP 7.3 พร้อมกับ lib สำหรับ MariaDB (MySQL)
$ sudo apt install php7.3-fpm php7.3-mysql
  • สามารถตรวจสอบ version ของโปรแกรมต่างๆ ตามนี้
$ /usr/sbin/nginx -v
$ mariadb --version
$ php --version

ตัวอย่าง
$ /usr/sbin/nginx -v && mariadb --version && php --version
nginx version: nginx/1.14.2
mariadb  Ver 15.1 Distrib 10.3.15-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2
PHP 7.3.4-2 (cli) (built: Apr 13 2019 19:05:48) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.4, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.3.4-2, Copyright (c) 1999-2018, by Zend Technologies
  • เมื่อได้ติดตั้งโปรแกรมทั้ง 3 เสร็จเรียบร้อย ก็ต้องตั้งค่าให้ nginx ทำงานกับ php โดยเพิ่มใน site configuration ตัวอย่างเช่น /etc/nginx/sites-available/default
server {
 listen 80 default_server;
 listen [::]:80 default_server;root /var/www/html;index index.php index.html index.htm index.nginx-debian.html;server_name _;location / {
  # First attempt to serve request as file, then
  # as directory, then fall back to displaying a 404.
  try_files $uri $uri/ =404;
 }
 #
 # pass PHP scripts to FastCGI server
 location ~ \.php$ {
  include snippets/fastcgi-php.conf;
  fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
 }
 # deny access to .htaccess files, if Apache's document root
 # concurs with nginx's one
 #
 location ~ /\.ht {
  deny all;
 }
}
  • ทำการทดสอบ config ใหม่ และ restart nginx
$ sudo service nginx testconfig
[ ok ] Testing nginx configuration:.
$ sudo systemctl restart nginx
  • ทำการทดสอบเวปไซต์ php อาจจะสร้างไฟล์ง่ายๆ check.phpเรียก phpinfo(); หากสามารถเปิด php นี้ได้ ก็เป็นอันเรียบร้อย
<?php phpinfo(); ?>

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

ความคิดเห็นคำแนะนำของท่านต่อบทความนี้ จะถูกนำมาใช้ในการพัฒนาปรับปรุงงานเขียนของผม อย่างไงก็รบกวนแสดงความเห็นคิดด้วยนะครับ

สนับสนุนการเงินได้ที่

  • บัญชีกสิกรไทย 003–3–29344–5