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

29 tháng 4, 2020
3 phút đọc
Hướng dẫn cài đặt Nginx + MySQL + PHP (LEMP) trên Ubuntu 18

Bài viết này dành cho những ai cần triển khai PHP mà không muốn sử dụng Apache, thay vào đó thích dùng Nginx hơn. Ngoài ra, mình cũng ghi lại các bước này để phòng khi sau này chính mình cần đến. 😄


1. Nginx#

Nguồn tham khảo: https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-ubuntu-18-04

$ sudo apt update
$ sudo apt install nginx
$ sudo ufw allow 'Nginx Full'

Check cài đặt thành công chưa?

$ systemctl status nginx

Lúc này bạn có thể truy cập bằng trình duyệt tới đường dẫn IP của server tại port 80 (hoặc gõ tên domain nếu đã gắn domain), nó sẽ hiển thị trang landing page mặc định của Nginx.

Trang landing này nằm ở thư mục /var/www/html, bạn có thể edit lại source code html của nó tùy theo nhu cầu của mình 😄


2. MySQL#

Cài đặt MySQL và tạo user root:

$ sudo apt install mysql-server
$ sudo mysql_secure_installation

Kiểm tra MySQL có chặn kết nối từ remote không nhé, nếu có thì mình thiết lập bỏ tính năng đó

$ sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
# bind-address = 127.0.0.1
# Xóa/Comment cái dòng bên trên lại nha
$ sudo service mysql restart
$ sudo ufw allow 3306/tcp

(Nếu bạn không dùng Wordpress, hãy bỏ qua và chuyển sang bước 3)

$ mysql -u root -p

Nhập password của user root vừa tạo, sau đó tạo user và DB dành riêng cho Wordpress:

mysql> CREATE DATABASE wordpress DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
mysql> GRANT ALL ON wordpress.* TO 'wordpressuser'@'%' IDENTIFIED BY 'password';

3. PHP#

$ sudo add-apt-repository universe
$ sudo apt install php-fpm php-mysql

Tích hợp PHP với Nginx:

$ sudo nano /etc/nginx/sites-available/example.com

Bạn có thể thay example.com bằng bất cứ tên nào mình tùy thích, tốt nhất là đặt theo domain hoặc tên của project, vì có thể sau này mình sẽ gặp trường hợp deploy nhiều project/domain trên cùng 1 máy.

server {
        listen 80;
        root /var/www/html;
        index index.php index.html index.htm index.nginx-debian.html;
        server_name example.com;

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

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
        }

        location ~ /\.ht {
                deny all;
        }
}
$ sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
$ sudo unlink /etc/nginx/sites-enabled/default

Test config và reload nếu không có lỗi:

$ sudo nginx -t
$ sudo systemctl reload nginx

Đến đây là đã kết thúc, mình chạy Wordpress trên subdomain nên nếu bạn nào tham khảo cấu hình cho subdomain thì có thể xem bên dưới nha:

server {
    listen 80;

    gzip on;
    gzip_types application/javascript image/* text/css;
    gunzip on;

    root /var/www/techdogy.com/blog;
    index index.php;

    server_name blog.techpago.com www.blog.techpago.com;

    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }

    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }

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

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.2-fpm.sock;
    }

    location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
        expires max;
        log_not_found off;
    }

}

Khám phá thêm