Home > Tiếng Việt > Hướng Dẫn (Tutorials) > Hướng dẫn cài đặt LEMP (NGINX-PHP-MYSQL) trên Ubuntu 14

Hướng dẫn cài đặt LEMP (NGINX-PHP-MYSQL) trên Ubuntu 14

Bước 1: Cài đặt Apache

Tiến hành cập nhật gói phần mềm của Ubuntu lên phiên bản mới nhất với lệnh:

# apt-get update -y

Lệnh để cài đặt nginx là:

# apt-get install nginx -y

Trong Ubuntu 14.04, Nginx được cấu hình để bắt đầu chạy ngay sau khi cài đặt.

Ta truy cập vào http://ip-vps nếu có hình như sau là thành công

Bước 2.Cài đặt PHP

# apt-get install php5-fpm php5-mysql -y

Cấu hình PHP

Mở tập tin cấu hình 'php5-fpm'

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

Ấn ctrl+w ,Tìm chữ 'cgi.fix_pathinfo' rồi thay số 1 = 0

cgi.fix_pathinfo=0


Bây giờ, chúng ta chỉ cần khởi động lại  PHP , bằng cách gõ:

# service php5-fpm restart

Cấu hình Nginx để sử dụng PHP

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

Mở tập tin cấu hình mặc định của máy chủ Nginx bằng cách gõ:

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

Trong file 'default' sẽ có nội dung mặc định đại khái như sau :

server {
        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;

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

        # Make site accessible from http://localhost/
        server_name localhost;

        location / {             
                try_files $uri $uri/ =404;           
         }  
}

Chúng ta cần phải thực hiện một số thay đổi tập tin như sau

server {
        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;

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

        # Make site accessible from http://localhost/
        server_name localhost;

        location / {             
                try_files $uri $uri/ =404;           
         }  

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

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

}

Khởi động lại Nginx 

# service nginx restart

Để kiểm tra php hoạt động không ta tạo 1 file info.php

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

Với nội dung như sau:
<?php
phpinfo();
?>

 

Bây giờ, bạn có thể truy cập trang này trong trình duyệt web của bạn bằng cách truy cập http:/IP-VPS/info.php 

Chúng tôi khuyên sau khi test xong php nên xóa file này info.php đi vì một số lý do bảo mật

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

Bước 3:Cài đặt MySQL Server

Gõ lệnh dưới đây để cài đặt MySQL 

# apt-get install mysql-server -y

Trong khi cài đặt, MySQL sẽ hiển thị một giao diện để bạn thiết lập mật khẩu root cho MySQL

Một khi bạn đã cài đặt MySQL, chúng ta nên kích hoạt nó bằng lệnh này:

# mysql_install_db

Chạy lệnh sau để thiết lập cho Mysql

# mysql_secure_installation

Nó sẽ hiện ra danh sách sau

root@nguyenvietduc:~# mysql_secure_installation
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):    (nhập mật khẩu vừa thiết lập ở trên)
OK, successfully used password, moving on...

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

You already have a root password set, so you can safely answer 'n'.

Change the root password? [Y/n] n                         (nếu muốn thay đổi mật khẩu thì ấn y , không thay đổi thì ấn n)
 ... skipping.

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                     (xóa người dùng vô danh)
 ... 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                   (Không cho phép đăng nhập root từ xa)
 ... 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       (xóa cơ sở dữ liệu kiểm tra và truy cập vào nó)
 - Dropping test database...
ERROR 1008 (HY000) at line 1: Can't drop database 'test'; database doesn't exist
 ... Failed!  Not critical, keep moving...
 - 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                       (khởi động lại) 
 ... 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!

Đăng nhập vào MySQL

# mysql -u root -p