Hướng dẫn cài đặt LAMP (Linux, Nginx, MariaDB, PHP) trên CentOS 6/7

LAMP server là một server chạy Linux, Nginx (đọc là Engine x), MySql và PHP (hoặc Perl/Python). Nó tương tự như LAMP server ngoại trừ việc web server nền tảng được giám sát bằng Nginx thay vì Apache.
sua may tinh tai nha
install-lemp-on-centos-6-7

Trong bài viết này mình sẽ hướng dẫn các bạn cách cài đặt bộ webserver nổi tiếng LAMP bao gồm:

Linux + Nginx + MariaDB + PHP (PHP-FPM) trên CentOS 7/6.5/6.4/6.3/6.2/6.1/6/5.10.

Nginx (đọc là Engine x) là một webserver nhỏ gọn, ít tốn RAM, mạnh mẽ và có hiệu suất hoạt động cao. PHP hỗ trợ các phiên bản 5.3, 5.4, 5.5, 5.6. MariaDB là phiên bản mã nguồn mở của MySQL, hoạt động giống hệt MySQL.

sua may tinh quan 8

Bắt đầu từ CentOS 7 mặc định đã bắt đầu hỗ trợ MariaDB, các bạn nên sử dụng thay cho MySQL ngay từ bây giờ. I.

Cài đặt Nginx và PHP trên CentOS 7/6.5/5.10

Đầu tiên bạn cần chuẩn bị một server CentOS mới tinh chưa cài gì cả. Kiểm tra lại xem hostname và file host đã chính xác chưa trước khi bắt đầu.

1. Thêm repo cần thiết

CentOS 7/6.5/5.10 Remi repository


## CentOS 7 ##
rpm –Uvh http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-5.noarch.rpm
rpm –Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
## CentOS 6 ##
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
## CentOS 5 ##
rpm –Uvh http://dl.fedoraproject.org/pub/epel/5/i386/epel-release-5-4.noarch.rpm
rpm –Uvh http://rpms.famillecollet.com/enterprise/remi-release-5.rpm
Nếu bạn gặp thông báo lỗi File Not Found, có thể phiên bản RPM đã thay đổi. Bạn có thể lấy link phiên bản mới nhất ở trang wiki Fedora EPEL

CentOS 7/6.5/5.10 Nginx repository

CentOS 7/6.5/5.10


## PHP 5.3 ##
yum install –y nginx php–fpm php–common
## PHP 5.4 ##
yum —enablerepo=remi install –y nginx php–fpm php–common
## PHP 5.5 ##
yum —enablerepo=remi,remi–php55 install –y nginx php–fpm php–common
## PHP 5.6 ##
yum —enablerepo=remi,remi–php56 install –y nginx php–fpm php–common
3. Cài đặt PHP module
Một số module PHP thông dụng:

OPcache (php-opcache) – The Zend OPcache provides faster PHP execution through opcode caching and optimization.
APCu (php-pecl-apc) – APCu userland caching
CLI (php-cli) – Command-line interface for PHP
PEAR (php-pear) – PHP Extension and Application Repository framework
PDO (php-pdo) – A database access abstraction module for PHP applications
MySQL (php-mysqlnd) – A module for PHP applications that use MySQL databases
PostgreSQL (php-pgsql) – A PostgreSQL database module for PHP
MongoDB (php-pecl-mongo) – PHP MongoDB database driver
SQLite (php-pecl-sqlite) – Extension for the SQLite Embeddable SQL Database Engine
Memcache (php-pecl-memcache) – Extension to work with the Memcached caching daemon
Memcached (php-pecl-memcached) – Extension to work with the Memcached caching daemon
GD (php-gd) – A module for PHP applications for using the gd graphics library
XML (php-xml) – A module for PHP applications which use XML
MBString (php-mbstring) – A module for PHP applications which need multi-byte string handling
MCrypt (php-mcrypt) – Standard PHP module provides mcrypt library support
Để cài đặt bạn hãy sử dụng lệnh yum --enablerepo=remi,remi-php56 install ten_module. Ví dụ:

1
yum —enablerepo=remi,remi–php56 install –y php–opcache php–pecl–apcu php–cli php–pear php–pdo php–mysqlnd php–pgsql php–pecl–mongo php–pecl–sqlite php–pecl–memcache php–pecl–memcached php–gd php–mbstring php–mcrypt php–xml
4. Stop httpd (Apache) server, Start Nginx và PHP-FPM
Stop httpd (Apache)


## CentOS 7 ##
systemctl stop httpd.service
## CentOS 6.5/5.10 ##
service httpd stop
Start Nginx


## CentOS 7 ##
systemctl start nginx.service
## CentOS 6.5/5.10 ##
service nginx start
Start PHP-FPM


## CentOS 7 ##
systemctl start php–fpm.service
## CentOS 6.5/5.10 ##
service php–fpm start
5. Tự động khởi động Nginx, PHP-FPM và tắt httpd
Tắt httpd (Apache) khi boot


## CentOS 7 ##
systemctl disable httpd.service
## CentOS 6.5/5.10 ##
chkconfig httpd off
Autostart Nginx


## CentOS 7 ##
systemctl enable nginx.service
## CentOS 6.5/5.10 ##
chkconfig —add nginx
chkconfig —levels 235 nginx on
Autostart PHP-FPM


## CentOS 7 ##
systemctl enable php–fpm.service
## CentOS 6.5/5.10 ##
chkconfig —add php–fpm
chkconfig —levels 235 php–fpm on
6. Cấu hình Nginx và PHP-FPM
Cấu hình Nginx

– Thay đổi worker_processes

1
nano /etc/nginx/nginx.conf
Chỉnh worker_processes bằng với số processor VPS của bạn

– Cấu hình nginx virtual hosts

1
nano /etc/nginx/conf.d/default.conf
Bạn thay đổi thông tin như bên dưới:



#
# The default server
#
server {
    listen       80;
    server_name example.com;
    location / {
        root   /usr/share/nginx/html;
        index index.php index.html index.htm;
        try_files $uri $uri/ /index.php?q=$uri&$args;
    }
    error_page  404              /404.html;
    location = /404.html {
        root   /usr/share/nginx/html;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        root           /usr/share/nginx/html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}
Các đoạn bôi đỏ là cần phải thay đổi.

– Restart Nginx

1
2
3
4
5
## CentOS 7 ##
systemctl restart nginx.service
## CentOS 6.5/5.10 ##
service nginx restart
Cấu hình PHP-FPM

– Chỉnh user và group

1
nano /etc/php–fpm.d/www.conf
Thay user và group = apache sang nginx


; 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
[...]
– Restart PHP-FPM

1
2
3
4
5
## CentOS 7 ##
systemctl restart php–fpm.service
## CentOS 6.5/5.10 ##
service php–fpm restart
7. Test cấu hình Nginx và PHP-FPM
1
nano /usr/share/nginx/html/info.php
Thêm đoạn sau vào


<?php
phpinfo();
?>
Test thử bằng link: http://<ip-address>/info.php. Nếu bạn thấy thông tin về PHP hiện ra thì đã cài đặt thành công.

Lưu ý: nếu bạn truy cập thẳng vào IP mà báo lỗi không kết nối được thì hãy open port http:


service iptables start
iptables –I INPUT –p tcp —dport 80 –j ACCEPT
service iptables save
service iptables restart
II. Cài đặt MariaDB trên CentOS 7/6.5/5.10
1. Thêm MariaDB repo
Lưu ý: Không cần thực hiện trên CentOS 7. Phiên bản MariaDB trên CentOS 7 hiện nay là 5.5.37

1
/usr/bin/mysql_secure_installation
Ngay bước đầu tiên bạn sẽ bị hỏi root password, do mới cài đặt nên tất nhiên chưa có password, nhấn Enter để tiếp tục.

– Output tương tự như sau:
Như vậy là bạn đã hoàn thành việc cài đặt LEMP stack trên CentOS rồi đó.

Nhận xét