ติดตั้งและคอนฟิก nginx บน CentOS 7 และ build angular ขึ้น nginx

ที่มา https://spalinux.com/2015/11/install-and-configure-nginx-on-centos-7

 

ติดตั้ง nginx  คำสั่งตามนี้

yum install epel-release

yum install nginx

systemctl start nginx
หาก start ไม่ได้ check port เครื่องดีๆ ว่าซ้ำไหม
systemctl enable nginx

จากนั้นลองทดสอบดู เข้าผ่านเว็บ

ไฟล์คอนฟิกหลักของ nginx คือไฟล์ /etc/nginx/nginx.conf

vi /etc/nginx/nginx.conf
...
http {
    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;
        ...
    }
    ...
}

ติดตั้งและคอนฟิกให้ NGINX รองรับการเขียนโปรแกรม PHP

ใช้คำสั่ง yum ติดตั้งแพ็กเกจ php-fpm

[root@cent7 ~]# yum install php-fpm

แก้ไขไฟล์ php.ini เพื่อปิดคุณสมบัติ cgi.fix_pathinfo เพิ่มความปลอดภัยให้กับเว็บเซิร์ฟเวอร์

[root@cent7 ~]# vi /etc/php.ini
...
; 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=0

แก้ไขไฟล์คอนฟิกของเซอร์วิส php-fpm โดยระบุชื่อไฟล์ socket พร้อมกับแก้ไขสิทธิ์ต่างๆ ของไฟล์ สำหรับรองรับการติดต่อกับเว็บเซิร์ฟเวอร์ ซึ่งในที่นี้ก็คือ nginx

[root@cent7 ~]# vi /etc/php-fpm.d/www.conf
; Start a new pool named 'www'.
[www]

; The address on which to accept FastCGI requests.
; Valid syntaxes are:
;   'ip.add.re.ss:port'    - to listen on a TCP socket to a specific address on
;                            a specific port;
;   'port'                 - to listen on a TCP socket to all addresses on a
;                            specific port;
;   '/path/to/unix/socket' - to listen on a unix socket.
; Note: This value is mandatory.
;listen = 127.0.0.1:9000
listen = /var/run/php-fpm/php-fpm.sock
...
; Set permissions for unix socket, if one is used. In Linux, read/write
; permissions must be set in order to allow connections from a web server. Many
; BSD-derived systems allow connections regardless of permissions.
; Default Values: user and group are set as the running user
;                 mode is set to 0666
listen.owner = nobody
listen.group = nobody
;listen.mode = 0666

; 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 = nginx
; RPM: Keep a group allowed to write in log dir.
group = nginx

ใช้คำสั่ง systemctl start เพื่อรันเซอร์วิส php-fpm

[root@cent7 ~]# systemctl start php-fpm

ถ้าแก้ไขถูกต้อง หลังจากรันเซอร์วิส php-fpm ลองใช้ ps ดูโปรเซส จะเห็นว่า php-fpm จะถูกรันด้วยผู้ใช้ชื่อ nginx

[root@cent7 ~]# ps -ef | grep php-fpm
root      2381     1  0 20:01 ?        00:00:00 php-fpm: master process (/etc/php-fpm.conf)
nginx     2382  2381  0 20:01 ?        00:00:00 php-fpm: pool www
nginx     2383  2381  0 20:01 ?        00:00:00 php-fpm: pool www
nginx     2384  2381  0 20:01 ?        00:00:00 php-fpm: pool www
nginx     2385  2381  0 20:01 ?        00:00:00 php-fpm: pool www
nginx     2386  2381  0 20:01 ?        00:00:00 php-fpm: pool www

และต้องมีไฟล์ socket ถูกสร้างขึ้น

[root@cent7 ~]# ls -l /var/run/php-fpm/php-fpm.sock
srw-rw-rw-. 1 nobody nobody 0 Nov 10 20:01 /var/run/php-fpm/php-fpm.sock

แก้ไขคอนฟิกไฟล์ของ nginx โดยเพิ่มคอนฟิกส่วน location ~ \.php$ เข้าไปภายใต้คอนฟิกส่วน server {}

[root@cent7 ~]# vi /etc/nginx/nginx.conf
...
http {
    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

        location ~ \.php$ {
            fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
        ...
    }
}

ใช้คำสั่ง systemctl restart รีสตาร์ตเซอร์วิส nginx เพื่อให้คอนฟิกที่แก้ไขมีผล

[root@cent7 nginx]# systemctl restart nginx

ลองเขียนไฟล์เพื่อทดสอบการรัน php

[root@cent7 ~]# vi /usr/share/nginx/html/test.php
<?php
phpinfo();
?>

แล้วทดลองใช้ browser เปิดหน้าเว็บ โดยระบุชื่อไฟล์ php ที่สร้างขึ้น
n02-nginx-with-php

เท่านี้ก็เราก็ได้ nginx ทำหน้าที่เป็นเว็บเซิร์ฟเวอร์แล้ว

หากต้องการจะลง php โมดูลส่วนอื่นๆ ก็ทำได้โดยติดตั้งแพ็กเกจ php อื่นๆ เพิ่มเติมเข้าไป แต่ถ้าจะให้โมดูล php มีผล ต้องรีสตาร์ตเซอร์วิส php-fpm

ตัวอย่างติดตั้ง php-mysql เพิ่มเติม

[root@cent7 ~]# yum install php-mysql

รีสตาร์ตเซอร์วิส php-fpm

[root@cent7 ~]# systemctl restart php-fpm


######################################################################################
หากต้องการ angular ขึ้น nginx  
ให้ไปที่โฟเดอร์ angular  ที่จะนำขึ้น แล้วใช้คำสั่ง

ng build --prod

prod จะเป็นการตรวจสอบ error ทั้งหมด เหมาะกับการ นำขึ้น production

จากนั้นจะได้ไฟเดอรฺ์ dist ให้ copy ทั้งหมดใน ไฟเดอรฺ์ dist  ไปวางที่ /usr/share/nginx/html
จากนั้นทดสอบดูอีกทีครับ

 หากทำการ เข้า index.php ไม่ได้ ให้ไปดูที่  /etc/nginx/nginx.conf
location / {
    index index.php index.html index.htm;
    try_files $uri $uri/ =404;
}
 

เคยเจอ error ของ session php ว่า
PHP message: PHP Warning: Unknown: Failed to write session data (files). Please verify that the current setting of 
session.save_path is correct (/var/lib/php/session) in Unknown on line 0" while reading upstream, client: 10.254.172.52, server: _, 
request: "GET /phpPgAdmin/redirect.php?subject=server&server=127.0.0.1%3A5432%3Aallow HTTP/1.1", upstream: "fastcgi://unix:/var/run/php-fpm/php-fpm.sock:", 
host: "10.254.172.6", referrer: "http://10.254.172.6/phpPgAdmin/browser.php"

เจอใน log /var/log/nginx/error.log
วิธีแก้คือ ไปสร้าง var/lib/php/session เพราะไม่มีอยู่ จากนั้น ให้ chmod 777 session ด้วย และทดสอบใช้อีกที
หากเจอ error ว่า nginx: [emerg] socket() [::]:80 failed (97: Address family not supported...ocol)
จากที่หาข้อมูล พบว่า เครื่อง server ที่ติดตั้งไป ไม่มี ipv6 เลยต้อง แก้ ที่ config ที่
/etc/nginx/nginx.conf แล้วไปที่ listen       [::]:80 default_server;
แล้ว คอมเม้นไว้ ตามนี้ #listen       [::]:80 default_server;
ส่วนไฟล์ nginx.conf หากเป็น os อืนๆ ที่อยู่และ ชื่อไฟล์จะต่างกันนะ จากนั้นลอง  start nginx ดู

ใส่ความเห็น

อีเมลของคุณจะไม่แสดงให้คนอื่นเห็น ช่องที่ต้องการถูกทำเครื่องหมาย *