Home » Categories » VPS » VPS Linux

Hướng dẫn cài đặt LEMP (Linux, Nginx, MySQL, PHP) trên Ubuntu 12.04

Giới thiệu nginx

Nginx là một web server nguồn mở hoạt động nhanh và sử dụng ít tài nguyên hệ thống hơn Apache. Khi ở trạng thái tải nhẹ thì sự khác nhau giữa Apache và Nginx là không đáng kể. Tuy vậy, ở trạng thái tải nặng, Nginx có thể mở rộng sao cho phù hợp và hoạt động nhanh mà không cần chiếm quá nhiều tài nguyên bộ nhớ như Apache.

Các blog của mình trước đây chạy trên nền tảng LAMP (Linux, Apache, MySQL, PHP) nhưng từ khi chuyển sang LEMP thì thấy tài nguyên sử dụng giảm hơn trước khá nhiều.

lemp

Các bước cài đặt LEMP trên Ubuntu

Đầu tiên bạn cần chuẩn bị một server Ubuntu mới tinh chưa cài gì cả, ở đây mình dùng Ubuntu 12.04 64 bit. Kiểm tra lại xem hostname và file host đã chính xác chưa.

1. Update Apt-Get

sudo apt-get update

2. Cài đặt MySQL

sudo apt-get install mysql-server php5-mysql

Trong quá trình cài đặt bạn sẽ phải cài đặt root mysql password

Sau khi cài đặt MySQL, kích hoạt bằng lệnh:

sudo mysql_install_db

Hoàn tất cài đặt

sudo /usr/bin/mysql_secure_installation

Điền vào root password

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

Sau đó là các thao tác như cài đặt lại password, xóa anonymous user… Cái này tùy bạn setup

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] y                                            
 ... 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] y
... 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] y
 - 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] y
 ... Success!

Cleaning up...

Đường dẫn file cài đặt MySQL: /etc/my.cnf

3. Cài đặt nginx

sudo apt-get install nginx

Chạy nginx

sudo service nginx start

Lúc này bạn có thể truy cập thẳng vào IP/domain để xem nginx đã hoạt động hay chưa

4. Cài đặt PHP

sudo apt-get install php5-fpm

5. Tùy chỉnh PHP

Tùy chỉnh lại php.ini

sudo nano /etc/php5/fpm/php.ini

Tìm dòng cgi.fix_pathinfo=1 (nhấn Ctrl+W trong nano), bỏ dấu ; ở đầu và thay 1 = 0

cgi.fix_pathinfo=0

Lưu lại (Ctrl+O, Enter) và thoát (Ctrl+X)

Tùy chỉnh php5-fpm

sudo nano /etc/php5/fpm/pool.d/www.conf

Tìm dòng listen = 127.0.0.1:9000 và thay 127.0.0.1:9000 thành /var/run/php5-fpm.sock

listen = /var/run/php5-fpm.sock

Lưu và thoát.

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

sudo service php5-fpm restart

6. Tùy chỉnh nginx

Mở file virtual host mặc định

sudo nano /etc/nginx/sites-available/default

Tìm và thay đổi cài đặt như bên dưới. Chú ý các phiên bản Ubuntu mới hơn sử dụng ‘htm’ thay vì ‘www’ nên bạn cần chỉnh lại cài đặt tương ứng.

[...]
server {
        listen   80;

        root /usr/share/nginx/www;
        index index.php index.html index.htm;

        server_name example.com;

        location / {
                try_files $uri $uri/ /index.php?$args;
        }

        error_page 404 /404.html;

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
              root /usr/share/nginx/www;
        }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        location ~ \.php$ {
                try_files $uri =404;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;

        }

}
[...]

Một số thay đổi:

  • Thêm index.php
  • Thay đổi server_name thành tên miền
  • Chỉnh sửa đường dẫn cho phù hợp với permalink của WP
  • Chỉnh lại nội dung trong phần cài đặt “location ~ \.php$ {“

Lưu và thoát.

7. Tạo php info file

Tạo file php.info

sudo nano /usr/share/nginx/www/info.php

Thêm nội dung

<?php
phpinfo();
?>

Lưu và thoát

Khời động lại nginx

sudo service nginx restart

Đã xong, giờ bạn có thể xem các cài đặt của nginx và php-fpm bằng cách truy cập: http://youripaddress/info.php

8. Cài đặt phpMyAdmin

sudo apt-get install phpmyadmin

Khi phpMyAdmin hỏi bạn chọn server (apache or lighttpd) thì chọn cái nào cũng được.

Tạo symbolic link

sudo ln -s /usr/share/phpmyadmin/ /usr/share/nginx/www

Sửa lỗi The mcrypt extension is missing khi chạy phpMyAdmin

Mở file php.ini

sudo nano /etc/php5/fpm/php.ini

Tìm đoạn Dynamic Extensions và thêm dòng sau vào phía cuối

extension=mcrypt.so

Restart php5-fpm

sudo service php5-fpm restart

Khởi động lại nginx

sudo service nginx restart

Giờ bạn đã có thể truy cập vào phpMyAdmin thông qua đường dẫn http://youripaddress/phpmyadmin

Attachments Attachments
There are no attachments for this article.
Comments Comments
There are no comments for this article. Be the first to post a comment.
Related Articles RSS Feed
Thủ thuật tối ưu LAMP server qua htaccess
Xem 684 - Đăng ngày Fri, Feb 26, 2016
Add thêm website trên VPS Apache CentOS
Xem 1098 - Đăng ngày Fri, Feb 26, 2016
Cập nhật phiên bản PHP 5.3.X lên 5.4.X hoặc 5.5.X trên CENTOS
Xem 3770 - Đăng ngày Wed, Mar 2, 2016
Hướng dẫn kết nối sFTP bằng FileZilla
Xem 2879 - Đăng ngày Sat, Feb 27, 2016
Cài đặt FTP Server đơn giản trên CentOS với ProFTPD
Xem 2741 - Đăng ngày Sat, Feb 27, 2016
Thay đổi Timezone trên CentOS theo đúng múi giờ Việt Nam
Xem 790 - Đăng ngày Thu, Aug 11, 2016
Thay đổi Timezone trên Ubuntu
Xem 864 - Đăng ngày Fri, Feb 26, 2016
Hướng dẫn Tạo DKIM tên miền trên Zimbra Mail Server
Xem 856 - Đăng ngày Fri, Apr 17, 2020
Cài đặt EPEL và REMI Repo trên CentOS 7/6/5
Xem 1193 - Đăng ngày Sat, Feb 27, 2016
Thay đổi Timezone trên CentOS
Xem 598 - Đăng ngày Fri, Feb 26, 2016