ติดตั้ง Nginx พร้อม PHP-FPM 5.5 จาก Source บน CentOS 7 เขียนเมื่อ 2014.08.21 โดย

เริ่มจาก update package ทั้งหมดในเครื่อเสียก่อน

yum update -y

จากนั้นทำการแก้ไข selinux โดยแก้ไขไฟล์ /etc/selinux/config

vi /etc/selinux/config

แก้ไข้บรรทัด

SELINUX=enforcing

เป็น

SELINUX=disabled

จากนั้นทำการบันทึก แล้ว restart

เมื่อเริ่มเครื่องใหม่ ก็สร้าง user ใหม่ ในที่นี้ก็ให้ชื่อ nginx

useradd nginx


ติดตั้ง Nginx 1.6.1

เริ่มจากติดตั้งโปรแกรมที่สำคัญสำหรับการ compile Nginx

yum install -y nano wget pcre-devel zlib-devel patch gcc gcc-c++ openssl-devel

ทำการดาวน์โหลด Nginx แตกไฟล์ให้เรียบร้อย

wget http://nginx.org/download/nginx-1.6.1.tar.gz
tar xf nginx-1.6.1.tar.gz
cd nginx-1.6.1

เริ่มทำการ compile และติดตั้ง

./configure --with-http_stub_status_module --with-file-aio --with-http_ssl_module
make && make install

สร้าง initscript

nano /etc/init.d/nginx

ใส่เนื้อหาตามนี้ แล้วทำการบันทึก

#!/bin/sh

# nginx - this script starts and stops the nginx daemin
#
#
#
# chkconfig:   - 85 15
#
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
#
#               proxy and IMAP/POP3 proxy server
#
# processname: nginx
#
# config:      /usr/local/nginx/conf/nginx.conf
#
# pidfile:     /usr/local/nginx/logs/nginx.pid
#
 
#
# Source function library.
. /etc/rc.d/init.d/functions
 
# Source networking configuration.
. /etc/sysconfig/network
 
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
 
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
 
lockfile=/var/lock/subsys/nginx

# do not edit, put changes in /etc/sysconfig/phpfastcgi
[ -f /etc/sysconfig/phpfastcgi ] && . /etc/sysconfig/phpfastcgi
 
start() {  
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

restart() {
    configtest || return $?
    stop
    start
}
 
reload() {
    configtest || return $?
    echo -n $"Reloading $prog: "
    killproc $nginx -HUP
    RETVAL=$?
    echo
}

force_reload() {
    restart
}
 
configtest() {
  $nginx -t -c $NGINX_CONF_FILE
}
 
rh_status() {
    status $prog
}
 
rh_status_q() {
    rh_status >/dev/null 2>&1
}

case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
            ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
        exit 2
esac
เปลี่ยน mode ให้ทำการได้
chmod +x /etc/init.d/nginx
เปิด port ที่ firewalld
firewall-cmd --permanent --zone=public --add-port=80/tcp
firewall-cmd --reload
หรือจะทำการปิดการทำงานของ firewalld ก็ได้
systemctl stop firewalld
systemctl disable firewalld


ติดตั้ง PHP 5.5 แบบ FPM

เริ่มที่ติดตั้ง EPEL repo
yum install -y epel-release
ติดตั้ง library ที่จำเป็น เนื่องจากครั้งนี้จะติดตั้งให้รองรับ IMAP ด้วย จึงเพิ่ม uw-imap-devel เข้าไป
yum install -y libxml2-devel openssl-devel mariadb-server mariadb mariadb-devel mariadb-libs bzip2-devel curl-devel gd-devel libjpeg-devel libpng-devel libmcrypt-devel net-snmp-devel libxslt-devel libtool-ltdl-devel libevent-devel systemd-devel libc-client uw-imap-devel libicu-devel
ดาวน์โหลด PHP ปัจจุบันเป็น 5.5.15 แล้วแตกไฟล์ให้เรียบร้อย
wget -O php-5.5.15.tar.gz http://th1.php.net/get/php-5.5.15.tar.gz/from/this/mirror
tar xf php-5.5.15.tar.gz cd php-5.5.15
ทำการ compile และติดตั้ง
./configure  --with-openssl --with-zlib --enable-bcmath --with-bz2 --enable-calendar --with-curl --enable-dba --enable-exif --enable-ftp --with-gd --enable-gd-native-ttf --enable-gd-jis-conv --enable-mbstring --with-mcrypt --with-mhash --with-mysql=/usr --with-mysql-sock=/var/lib/mysql/mysql.sock --with-mysqli=/usr/bin/mysql_config --with-pdo-mysql --enable-intl --enable-shmop --with-snmp --enable-soap --enable-sockets --with-regex --enable-wddx --with-xmlrpc --with-iconv --with-xsl --enable-zip --with-jpeg-dir=/usr/lib64 --with-freetype-dir=/usr/lib64 --libdir=/usr/lib64 --with-libdir=lib64 --enable-fpm --with-fpm-systemd --enable-opcache --enable-pcntl --enable-zip --with-imap --with-imap-ssl --with-kerberos
make && make install
จากนั้น copy ไฟล์ php.ini
cp php.ini-production /usr/lib64/php.ini
copy ไฟล์ systemd service script สำหรับ php-fpm
cp sapi/fpm/php-fpm.service /usr/lib/systemd/system/
แก้ไข path ต่างๆ ให้เรียบร้อย
nano /usr/lib/systemd/system/php-fpm.service
ให้เป็นเนื้อหาตามนี้
[Unit]
Description=The PHP FastCGI Process Manager
After=syslog.target network.target

[Service]
Type=notify
PIDFile=/var/run/php-fpm.pid
ExecStart=/usr/local/sbin/php-fpm --nodaemonize --fpm-config /usr/local/etc/php-fpm.conf
ExecReload=/bin/kill -USR2 $MAINPID

[Install]
WantedBy=multi-user.target
เปิดการใช้งานทุกครั้งที่ reboot
systemctl enable php-fpm
ติดตั้งไฟล์ config ของ php-fpm
cp /usr/local/etc/php-fpm.conf.default /usr/local/etc/php-fpm.conf

แก้ไขไฟล์ /usr/local/etc/php-fpm.conf ในส่วนของ pool ให้เป็นตามนี้

user = nginx
group = nginx
listen = /var/run/php-fpm.sock
listen.owner = nginx
listen.group = nginx
listen.mode = 0660

เปิดใช้งาน opcache โดยเพิ่มเข้าไปในส่วนท้ายของไฟล์ /usr/lib64/php.ini

zend_extension=/usr/lib64/extensions/no-debug-non-zts-20121212/opcache.so


เริ่มการทำงานของ Nginx

service nginx start

เริ่มการทำงานของ php-fpm

systemctl start php-fpm.service

หรือ

service php-fpm start




คำเตือนคำเตือน เนื้อหาต่างๆ ในบทความ รวมถึงรูปภาพทั้งหมดในบทความนี้ เป็นความเห็นส่วนตัวของผู้เขียนแต่ละคน ซึ่งแต่ละคนได้ทำการลงทะเบียน และเขียนบทความลงใน Modoeye Articles นี้โดยไม่มีค่าธรรมเนียมใดๆ บทความเหล่านี้เป้าหมายเพื่อการศึกษา และความบันเทิงเท่านั้น การนำส่วนหนึ่งส่วนใดของบทความไปใช้งาน ควรทำการอ้างอิงถึงผู้เขียนและแหล่งที่มาด้วย