Cài đặt Nginx, PHP 5.3, MySQL (LEMP) trên CentOS 6.x

Tác giả server360, T.Chín 21, 2015, 09:06:32 CHIỀU

« Chủ đề trước - Chủ đề tiếp »

0 Thành viên và 1 Khách đang xem chủ đề.

Cài đặt Nginx, PHP 5.3, MySQL (LEMP) trên CentOS 6.x


Để cài đặt nginx, PHP 5.3, MySQL (LEMP) trên CentOS 6.x, các bước tương tự như Cài đặt Apache, PHP 5.3, MySQL (LAMP) trên CentOS 6.x. Tuy nhiên có đôi chút khác biệt khi cài và cấu hình nginx, cụ thể các bạn đọc bên dưới.
Nội dung giống bài cũ mình xin phép được copy cho nhanh. Sau khi cài đặt xong là có thể chạy WordPress.

1. Cài Repository

Repository (nhà kho) thường được gọi tắt là Repo, theo cách diễn đạt của mình Repo là một thư viện (máy chủ) chứa những tệp tin cần thiết để cài đặt hoàn chỉnh một phần mềm trong CentOS. Khi cài đặt một phầm mềm, hệ thống sẽ truy vấn vào thư viện này để tìm những file cần thiết để cài đặt một cách tự động.

Thông thường Repo mặc định của CentOS không có nhiều phầm mềm, hoặc có nhưng không phải phiên bản mới nhất. Vì thế cần cài thêm 2 Repo bổ sung: EPEL và RPMForge cho CentOS trước.

Dùng lệnh wget để tải về:

Mã nguồn [Chọn]
wget https://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
wget https://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el6.rf.x86_64.rpm

Dùng lệnh rpm -ivh để cài đặt:

Mã nguồn [Chọn]
rpm -ivh epel-release-6-8.noarch.rpm rpmforge-release-0.5.3-1.el6.rf.x86_64.rpm
2. Cài nginx

Không giống như Apache, bạn phải tự tạo repo để có thể cài đặt bản nginx mới nhất. Tạo file nginx.repo bằng lệnh vi (Hướng dẫn sử dụng lệnh vi cơ bản):

Mã nguồn [Chọn]
vi /etc/yum.repos.d/nginx.repo
với nội dung:

Mã nguồn [Chọn]
[nginx]
name=nginx repo
baseurl=https://nginx.org/packages/centos/6/$basearch/
gpgcheck=0
enabled=1

Dùng lệnh yum để cài đặt nginx:

Mã nguồn [Chọn]
yum install nginx
Khởi động nginx bằng lệnh:

Mã nguồn [Chọn]
service nginx start
Tham khảo thêm về cách tạo nginx.repo cho các hệ điều hành khác.

3. Cài MySQL

Cài đặt nhanh bằng lệnh sau:

Mã nguồn [Chọn]
yum -y install mysql mysql-server
Cấu hình cho MySQL tự động khởi động:

Mã nguồn [Chọn]
chkconfig --levels 235 mysqld on
MySQL có tên dịch vụ là mysqld, khởi động nó lên:

Mã nguồn [Chọn]
service mysqld start
Chạy lệnh sau để cấu hình MySQL:

Mã nguồn [Chọn]
mysql_secure_installation
Làm theo các bước sau, chủ yếu là ấn ENTER:

Mã nguồn [Chọn]
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!

In order to log into MySQL to secure it, we'll need the current
password for the root user. If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.

Set root password? [Y/n] ENTER để đặt mật khẩu cho root
New password: Nhập mật khẩu mới
Re-enter new password: Nhập lại mật khẩu mới
Password updated successfully!
Reloading privilege tables..
... Success!

By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] ENTER
... Success!

Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] ENTER
... Success!

By default, MySQL comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] ENTER
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] ENTER
... Success!

Cleaning up...

All done! If you've completed all of the above steps, your MySQL
installation should now be secure.

Thanks for using MySQL!

4. Cài đặt và cấu hình PHP

Cài đặt PHP và các module cần thiết để chạy WordPress:

Mã nguồn [Chọn]
yum -y install php php-mysql php-gd php-imap php-ldap php-mbstring php-odbc php-pear php-xml php-xmlrpc
nginx không trực tiếp hỗ trợ PHP, mà phải thông qua PHP-FPM (FPM – FastCGI Process Manager). Vì thế cần cài thêm module php-fpm, cài thêm php-xcache (XCache) để cải thiện tốc độ xử lý cho PHP.

Mã nguồn [Chọn]
yum install php-fpm php-xcache
5. Cấu hình PHP.

Xem timezone của hệ thống:

Mã nguồn [Chọn]
cat /etc/sysconfig/clock
ZONE=Asia/Ho_Chi_Minh

Sau đó mở file /etc/php.ini sửa cgi.fix_pathinfo và date.timezone giống như sau:
    ...
    ; 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.
    ; https://www.php.net/manual/en/ini.core.php#ini.cgi.fix-pathinfo
    cgi.fix_pathinfo=0
    ...
    [Date]
    ; Defines the default timezone used by the date functions
    ; https://www.php.net/manual/en/datetime.configuration.php#ini.date.timezone
    date.timezone = "Asia/Ho_Chi_Minh"
    ...

6. Cấu hình PHP-FPM.

Mặc định PHP-FPM dùng địa chỉ 127.0.0.1:9000, cấu hình PHP-FPM chuyển sang dùng socket để giảm các thiểu các gói tin TCP. Mở:

Mã nguồn [Chọn]
vi /etc/php-fpm.d/www.conf
Tìm đoạn listen = 127.0.0.1:9000 và sửa thành:    ...
    ;listen = 127.0.0.1:9000
    listen = /var/run/php-fpm/php-fpm.sock
    ...

7. Cấu hình Nginx.

Theo mặc định, nginx dùng thư mục /usr/share/nginx/html làm thư mục chính, khác với /var/www/html của Apache. Mình sẽ cấu hình nginx sử dụng thư mục /var/www/html, đề phòng trường hợp đôi khi nginx có thể phát sinh lỗi và ngừng hoạt động. Lúc đó có thể bật Apache lên để dùng tạm mà không mất công chuyển mã nguồn web.

Mở file cấu hình virtual hosts bằng lệnh:

Mã nguồn [Chọn]
vi /etc/nginx/conf.d/default.conf
Thay thế với nội dung như sau:

Mã nguồn [Chọn]
server {
    listen      80;
    server_name   vietnetwork.vn www.vietnetwork.vn;
    root        /var/www/html;
    index       index.php index.html index.htm;

    if (!-e $request_filename)
    {
        rewrite ^(.+)$ /index.php?q=$1 last;
    }

    error_page 404 /index.php;

    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;
    }

    # deny access to apache .htaccess files
    location ~ /\.ht
    {
        deny all;
    }
}

8. Kiểm tra.

Khởi động lại nginx, php-fpm, và bật chế độ tự động khởi động cùng hệ thống.

Mã nguồn [Chọn]
chkconfig --levels 235 nginx on
service nginx restart
chkconfig --levels 235 php-fpm on
service php-fpm restart

Tạo file info.php để kiểm tra xem nginx đã chạy được PHP chưa, và PHP đã hỗ trợ MySQL hay chưa:

Mã nguồn [Chọn]
vi /var/www/html/info.php
Với nội dung:
Mã nguồn [Chọn]
<?php phpinfo(); ?>
Kết quả như hình là bạn đã cài đặt thành công:

9. Cài đặt phpMyAdmin

Đối với các bạn mới làm quen với CentOS thì tốt nhất nên sử dụng phpMyAdmin để quản lý cơ sở dữ liệu MySQL. Tải phpMyAdmin bản mới nhất tại đây, và upload vào /var/www/html.

Còn mình hay quản lý MySQL bằng lệnh, hoặc quản lý bằng Webmin rất ít khi dùng phpMyAdmin. Mình sẽ viết một bài riêng về cách quản lý MySQL của mình trên CentOS.