Home > Tiếng Việt > Hướng Dẫn (Tutorials) > Hướng dẫn cài đặt LEMP ( NGINX, PHP-FPM, MARIADB) và tạo VirtualHost trên centos 6

Hướng dẫn cài đặt LEMP ( NGINX, PHP-FPM, MARIADB) và tạo VirtualHost trên centos 6

 

I.Chuẩn bị

Cài đặt kho epel và remi . Tham khảo ở link sau Click here

Tắt selinux ,sửa file selinux

# nano /etc/sysconfig/selinux 

Sửa SELINUX=disabled

# reboot để khởi động lại máy

Mở cổng 80 và 3306 iptables 

# nano /etc/sysconfig/iptables

Và thêm vào 2 dòng

-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT

khởi động lại iptables

# service iptables restart

II.Tiến hành

Bước 1: Tạo kho để cài đặt nginx

Nếu bạn muốn dùng phiên bản nginx 1.10. thì phải tạo kho cài đặt , còn dùng bản nginx 1.0, thì không cần làm bước này

Tạo file nginx.repo

# nano /etc/yum.repos.d/nginx.repo

Trong file nginx.repo ta tạo nội dung sau

[nginx]

name=nginx repo

baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/

gpgcheck=0

enabled=1

Cho hệ thống update 

# yum update -y

Bước 2: cài đặt nginx và php-fpm

Dùng lệnh sau để cài đặt nginx  và php-fpm 

# yum install nginx php-fpm php-mysql php-common php-mbstring php-mcrypt php-gd -y

Một số tên gói có thể bạn muốn cài đặt thêm

  • php-pecl-memcached – Memcached Server
  • php-pecl-memcache – extension chạy Memcached dành cho PHP
  • php-opcache – Zend Opcache
  • apcu – APC Opcode Cache

Khởi động dịch vụ nginx

# service nginx start

Cho nginx khởi động cùng hệ thống

# chkconfig nginx on

khởi động service php-fpm

# service php-fpm start

Cho service php-fpm khởi động cùng hệ thống

# chkconfig php-fpm on

Các bạn có thể kiểm tra phiên bản NGINX và PHP-FPM qua lệnh sau

#  nginx -v
#  php-fpm -v

Bước 3: Cấu hình nginx và php-fpm

Cấu hinh php-fpm

# nano /etc/php-fpm.d/www.conf

Tìm và sửa các use và group

user = apache
; RPM: Keep a group allowed to write in log dir.
group =
apache

Sửa thành
user = nginx
; RPM: Keep a group allowed to write in log dir.
group =
nginx

Khởi động lại php-fpm

# service php-fpm restart

Sau khi thực hiện xong các bạn truy cập vào địa chỉ ip của máy mình (http://ip-vps) nếu có hình sau là thành công

Cấu hình nginx 

Ta đổi tên thư mục cấu hình mặc định thành 1 file dự phòng

#  mv /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.bk

Và tạo 1 file trống sau đó chèn nội dung như sau

# nano /etc/nginx/conf.d/default.conf 

Chèn nội dung như sau :

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;
        root   /usr/share/nginx/html;

    location / {

    }

    #error_page  404              /404.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;
    }
# Pressed handle PHP-FPM through PHP
location ~* \.php$ {
    fastcgi_index   index.php;
    fastcgi_pass    127.0.0.1:9000;
    #fastcgi_pass   unix:/var/run/php-fpm/php-fpm.sock;
    include         fastcgi_params;
    fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
    fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;

# Set the timeout for the proxy
    fastcgi_connect_timeout 60;
    fastcgi_send_timeout 180;
    fastcgi_read_timeout 180;
    fastcgi_buffer_size 512k;
    fastcgi_buffers 512 16k;
    fastcgi_busy_buffers_size 512k;
    fastcgi_temp_file_write_size 512k;
    fastcgi_intercept_errors on;
}

location = /robots.txt  { access_log off; log_not_found off; }
location = /favicon.ico { access_log off; log_not_found off; expires 30d; }
location ~ /\.          { access_log off; log_not_found off; deny all; }
location ~ ~$           { access_log off; log_not_found off; deny all; }
location ~ /\.git { access_log off; log_not_found off; deny all; }
location = /nginx.conf { access_log off; log_not_found off; deny all; }

location ~* \.(3gp|gif|jpg|jpeg|png|ico|wmv|avi|asf|asx|mpg|mpeg|mp4|pls|mp3|mid|wav|swf|flv|exe|zip|tar|rar|gz|tgz|bz2|uha|7z|doc|docx|xls|xlsx|pdf|iso|eot|svg|ttf|woff)$

{    gzip_static off;
    add_header Cache-Control "public, must-revalidate, proxy-revalidate";
    access_log off;
    expires max;
    break;
}

location ~* \.(css|js)$ {
    #add_header Pragma public;

    add_header Cache-Control "public, must-revalidate, proxy-revalidate";
    access_log off;
    expires 30d;
    break;
}
}

Sửa file /nginx.conf

# nano /etc/nginx/nginx.conf

Thêm dòng vàng sau vào 

access_log  /var/log/nginx/access.log  main;
index index.php  index.html index.htm;

Khởi động lại nginx 

# service nginx restart

Test cấu hình Nginx và PHP-FPM

Để test xem PHP-FPM đã hoạt động tốt chưa, hãy tạo 1 file tên info.php lưu trong thư mục html :

# nano /usr/share/nginx/html/info.php

Với nội dung là:

<?php
phpinfo();
?>

Sau đó truy cập vào http://ip-vps/info.php , nếu nó hiển thị ra trang PHP như ở dưới thì thành công.

Vì một số lý do bảo mật sau khi test thành công xong bạn nên xóa file info.php này đi để tránh cho người ngoài truy cập vào 

# rm -f /usr/share/nginx/html/info.php

Bước 4: Cài đặt MariaDB

Thêm kho MariaDB repo

Để cài đặt MariaDB, bạn cần thêm một repository sau vào hệ thống bằng cách chạy lệnh sau 

# wget -O /etc/yum.repos.d/MariaDB.repo http://mariadb.if-not-true-then-false.com/centos/$(rpm -E %centos)/$(uname -i)/10

Sau đó hãy xóa cache của yum đi

# yum clean all

update lại hệ thống để yum để nó cập nhật package của MariaDB

# yum update -y

Nếu bạn không làm 2 bước trên nó sẽ báo lỗi như sau 

Transaction Check Error:

file /usr/share/mysql/ukrainian/errmsg.sys from install of MariaDB-server-10.0.25-1.el6.x86_64 conflicts with file from package mysql-libs-5.5.50-1.el6.remi.x86_64

Error Summary
 

Cài đặt hoặc MariaDB

# yum install -y MariaDB MariaDB-server

Khởi động MariaDB

# service mysql start

Cho MariaDB khởi động cùng hệ thống

# chkconfig mysql on

Đặt mật khẩu cho tài khoản root MariaDB:

# mysql_secure_installation

Sẽ hiện ra các câu sau

câu 1 : Enter current password for root (enter for none)<Nhập mật khẩu hiện tại cho root (nhập cho không có)> : cứ enter là xong

câu 2 : Set root password? [Y/n]<Đặt mật khẩu gốc> chọn Y và nhập mật khẩu 

câu 3 : Remove anonymous users? [Y/n]<xóa người dùng vô danh> chọn Y

câu 4 : Disallow root login remotely? [Y/n]<Không cho phép đăng nhập root từ xa> chọn Y 

câu 5 : Remove test database and access to it? [Y/n]<xóa cơ sở dữ liệu kiểm tra và truy cập vào nó> chọn Y

câu 6 : Reload privilege tables now? [Y/n]<khởi động lại> Chọn Y

Để đăng nhập vào mariaDB

# mysql -u root -p

Một số câu lệnh của mariaDB

Hiển thị toàn bộ databases:

MariaDB [(none)]> SHOW DATABASES;

Tạo database:

MariaDB [(none)]> CREATE DATABASE tên cở sở dữu liệu;

Sử dụng một database:

MariaDB [(none)]> USE tên cở sở dữu liệu;

Xóa một database:

MariaDB [(none)]> DROP DATABASE tên cở sở dữu liệu;

Bước 5: Tạo VirtualHost 

Giả sử ta có domain là maxserver.net

Ta sẽ tạo một số file như sau

Tạo 1 thư mục chứa các file log

# mkdir -p /home/nginx/maxserver.net/log

Tạo 1 file ghi các lỗi

# touch /home/nginx/maxserver.net/log/error.log

Tạo thư mục chứa code của web

# mkdir -p /home/nginx/maxserver.net/public_html

Gán use và group 

# chown -R nginx:nginx /home/nginx

Tạo 1 file cấu hình riêng cho domain

# cp /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/maxserver.net.conf

Chỉnh sửa file maxserver.net.conf

# nano /etc/nginx/conf.d/maxserver.net.conf

sửa dòng sau 

server_name  localhost;

Sửa thành

server_name www.maxserver.net maxserver.net;

Thêm dòng vàng sau

    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;
   
error_log    /home/nginx/xobic.com/log/error.log error;

Tìm tất cả các dòng đường dẫn sau:

root   /usr/share/nginx/html;

Và sửa thành

root  /home/nginx/maxserver.net/public_html;

Khởi động lại nginx 

# service nginx restart

Test virtualhost bằng cách tạo 1 file 

# nano /home/nginx/maxserver.net/public_html/index.html

Vội dung như sau

<html>
  <head>
    <title>maxserver</title>
  </head>
  <body>
    <h1>Chuc mot ngay tot lanh, ban da tao virtualhost thanh cong</h1>
  </body>
</html>

Và truy cập vào http://ip-vps/index.html và xem kết quả