Skip to content

Nginx MariaDB PHP

WannaDrunk edited this page Sep 23, 2021 · 1 revision

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

คลิป Youtube: https://youtu.be/l5KdKqggDLo

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 apt search ^mariadb พบว่า MariaDB package ชื่อ mariadb-server-10.3
$ 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 nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

$ sudo systemctl restart nginx
  • ทำการทดสอบเวปไซต์ php อาจจะสร้างไฟล์ง่ายๆ check.phpเรียก phpinfo(); หากสามารถเปิด php นี้ได้ ก็เป็นอันเรียบร้อย
<?php phpinfo(); ?>

Clone this wiki locally