วันจันทร์ที่ 13 มกราคม พ.ศ. 2557

วิธีติดตั้ง Nginx, MySQL, PHP-FRM, phpMyAdmin (LEMP) บน CentOS


Nginx (Engine X) คือ Web Server ฟรี ประสิทธิภาพสูงรองรับการทำ reverse proxy ตลอดจน IMAP/POP3 Proxy Server โดย Nginx มีการพัฒนาในปี 2002 และเปิดตัวครั้งแรกในปี 2004 โดยตัว Nginx ถือว่าเป็น Web Server ที่มีประสิทธิภาพสูง และน่าสนใจเป็นอย่างมาก

PHP-FPM คือ FastCGI Process Manager เป็นอีกหนึ่งทางเลือกของ PHP FastCGI โดยตัว PHP-FPM นั้นถูกพัฒนาต่อยอดขึ้นมาเพื่อการประมวลผลเว็บไซต์ที่่มีความต้องการด้านประสิทธิภาพการทำงาน และความเร็วที่สูงกว่าปกติ

หมายเหตุ
บทความนี้จะสอนติดตั้ง Linux, Nginx, MySQL 5.5, PHP 5.1, phpMyAdmin 4.1 (LEMP) บน CentOS 6 แต่จะไม่ได้ลงรายละเอียดต่าง ๆ เช่น VirtualHost (VH) หรือ การปรับ Config Nginx เชิงลึก เป็นต้น

วิธีการติดตั้ง Linux CentOS 6


วิธีติดตั้ง Linux, Nginx, MySQL, PHP, phpMyAdmin (LEMP) บน Linux CentOS
1. ติดตั้ง Remi Repository
1
2
# rpm -Uvh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
# rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm

2. ติดตั้ง Nginx Repository
1
vi /etc/yum.repos.d/nginx.repo

เพิ่มข้อมูลเข้าไปในไฟล์ดังนี้
1
2
3
4
5
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1

3. ติดตั้ง Ngnix, MySQL 5.5, PHP 5.4 & PHP-FPM
1
yum --enablerepo=remi,remi-test install nginx mysql mysql-server php php-common php-fpm phpmyadmin

4. ติดตั้ง Modules ที่จำเป็นให้กับ PHP
1
yum --enablerepo=remi,remi-test install php-mysql php-pgsql php-pecl-mongo php-sqlite php-pecl-memcache php-pecl-memcached php-gd php-mbstring php-mcrypt php-xml php-pecl-apc php-cli php-pear php-pdo

5. Start Service Nginx, MySQL, PHP-FPM
1
2
3
/etc/init.d/nginx start
/etc/init.d/mysqld start
/etc/init.d/php-fpm start

6. แก้ไขไฟล์ config Nginx
1
vi /etc/nginx/conf.d/default.conf

แก้ไขเป็นดังนี้
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.php index.html index.htm;
    }

    error_page  404              /404.html;
    location = /404.html {
        root   /usr/share/nginx/html;
    }

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        root /usr/share/nginx/html;
        try_files $uri =404;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html$fastcgi_script_name;
        include        fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    location ~ /\.ht {
        deny  all;
    }
}

7. ลิ้งค์ phpMyAdmin เข้ากับ Directory Web ของ Nginx (Default Web Root)
1
2
cd /usr/share/nginx/html/
ln -s /usr/share/phpMyAdmin/ phpmyadmin

8. กำหนด Username และ Password ให้กับ MySQL
1
mysqladmin -u root password 01234

9. แก้ไขให้สามารถเข้าถึง phpMyAdmin ได้จากเครื่องอื่น ๆ
1
vi /etc/httpd/conf.d/phpMyAdmin.conf

แก้ไขเป็น
1
2
3
4
<Directory /usr/share/phpMyAdmin/>
    order allow,deny
    allow from all
</Directory>

10. สร้างไฟล์ php เพื่อทดสอบการติดตั้ง
1
vi /usr/share/nginx/html/phpinfo.php

ใส่ข้อมูลเข้าไปในไฟล์ดังนี้
1
2
3
<?php
phpinfo();
?>

11. Restart Nginx 
1
/etc/init.d/nginx restart

12. ปิด Firewall (ปิดเพื่อการทดสอบเท่านั้น)
1
/etc/init.d/iptables stop

13. ทดสอบการติดตั้งโดยเปิดโปรแกรมเว็บเบราว์เซอร์แล้วพิมพ์ http://localhost/phpinfo.php



14. ทดสอบการติดตั้ง phpMyAdmin โดยเปิดโปรแกรมเว็บเบราว์เซอร์แล้วพิมพ์ http://localhost/phpmyadmin



ไม่มีความคิดเห็น:

แสดงความคิดเห็น