Cấu hình Website Nginx CentOS

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

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

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

Cấu hình Website Nginx CentOS


Thông thường mỗi VPS mình chỉ chạy cho một website mà thôi, tuy nhiên trong trường hợp bạn có nhiều website và muốn add thêm chạy trên VPS thì sao, hãy tham khảo bài hướng dẫn này để thực hiện.


Giới thiệu Virtual Hosts

Virtual Hosts được sử dụng để giúp cho một VPS có thể chạy được nhiều website khác nhau.

Theo như nginx website, Virtual Hosts được gọi là Server Blocks trên nginx, tuy nhiên cho dễ dàng giống như Apache nên mình sẽ gọi là Virtual Hosts trong bài này. Các bước add thêm website trên VPS nginx chạy CentOS bằng Virtual Hosts như sau:

Chuẩn bị server

Đã cài đặt sẵn webserver nginx trên CentOS, có thể tham khảo bài viết cài đặt LEMP trên CentOS.

Tạo thư mục chứa website

Ví dụ mình sẽ tạo thư mục ở folder /var/www nhé

Mã nguồn [Chọn]
sudo mkdir -p /var/www/example.com/public_html

Chú ý thay example.com bằng domain của bạn.

Gán quyền

Đảm bảo cho website hoạt động bình thường

Mã nguồn [Chọn]
sudo chown -R nginx:nginx /var/www/example.com/public_html

Ngoài ra chmod 755 để đảm bảo mọi người có thể xem được website của bạn

Mã nguồn [Chọn]
sudo chmod 755 /var/www

Cài đặt Virtual Hosts

Mình sẽ chỉnh sửa file cấu hình mặc định của nginx

Mã nguồn [Chọn]
sudo nano /etc/nginx/conf.d/virtual.conf
Thêm đoạn sau vào cuối file:

Mã nguồn [Chọn]
#
# example.com configuration
#
server {
    listen       80;
    server_name example.com;

    location / {
        root   /var/www/example.com/public_html;
        index index.php  index.html index.htm;
        try_files $uri $uri/ /index.php?q=$uri&$args;
    }

    error_page  404              /404.html;
    location = /404.html {
        root   /var/www/example.com/public_html;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /var/www/example.com/public_html;
    }

    # pass the PHP scripts to FastCGI server
    #
    location ~ \.php$ {
        root           /var/www/example.com/public_html;
        fastcgi_pass   unix:/tmp/php5-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}

Lưu lại và thoát.

Restart nginx

Mã nguồn [Chọn]
sudo service nginx restart

Test thử domain

Giờ mình sẽ tạo thử file index.html để test thử domain example.com có hoạt động đúng không nhé.

Mã nguồn [Chọn]
sudo nano /var/www/example.com/public_html/index.html

Thêm đoạn code html vào file

Mã nguồn [Chọn]
<html>
  <head>
    <title>www.example.com</title>
  </head>
  <body>
    <h1>Success: You Have Set Up a Virtual Host</h1>
  </body>
</html>


Lưu và thoát.

Bây giờ bạn hãy test thử với link https://example.com, nếu kết quả hiện ra như bên dưới là thành công.

Cài đặt thêm Virtual Hosts

Để add thêm nhiều website nữa, bạn có thể lặp đi lặp lại bước. Cấu trúc file nginx config lúc nãy sẽ tương tự như sau:

Mã nguồn [Chọn]
#
# example.com configuration
#
server {
    listen       80;
    server_name example.com;

    location / {
        root   /var/www/example.com/public_html;
        index index.php  index.html index.htm;
        try_files $uri $uri/ /index.php?q=$uri&$args;
    }

    error_page  404              /404.html;
    location = /404.html {
        root   /var/www/example.com/public_html;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /var/www/example.com/public_html;
    }

    # pass the PHP scripts to FastCGI server
    #
    location ~ \.php$ {
        root           /var/www/example.com/public_html;
        fastcgi_pass   unix:/tmp/php5-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}

#
# example.net configuration
#
server {
    listen       80;
    server_name example.net;

    location / {
        root   /var/www/example.net/public_html;
        index index.php  index.html index.htm;
        try_files $uri $uri/ /index.php?q=$uri&$args;
    }

    error_page  404              /404.html;
    location = /404.html {
        root   /var/www/example.net/public_html;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /var/www/example.net/public_html;
    }

    # pass the PHP scripts to FastCGI server
    #
    location ~ \.php$ {
        root           /var/www/example.net/public_html;
        fastcgi_pass   unix:/tmp/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 là tất cả các website sẽ hoạt động.