Add thêm website trên VPS 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

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

Ví dụ mình sẽ tạo thư mục ở folder /home/ nhé. Thay tuanitpro.com bằng domain của bạn.

mkdir -p /home/www/tuanitpro.com/public_html

Gán quyền

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

chown -R nginx:nginx /home/www/tuanitpro.com/public_html

Cài đặt Virtual Hosts

Thêm file cấu hình .conf cho domain mới

nano /etc/nginx/conf.d/tuanitpro.com.conf

Sử dụng đoạn code sau:

#
# tunanitpro.com configuration
#
server {
    listen       80;
    server_name tuanitpro.com;
    root /home/www/tuanitpro.com/public_html;
    location / {
        
        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   /home/www/tuanitpro.com/public_html;
    }

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

    # pass the PHP scripts to FastCGI server
    #
    location ~ \.php$ {
        root           /home/www/tuanitpro.com/public_html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}

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

Reload Nginx

service nginx reload

Kết nối SSH đến VPS Vultr trên MacOS

Mặc dù có nhiều tool hỗ trợ việc quản lý VPS Linux/CentOS… tiện ích như WinSCP… nhưng đôi khi chúng ta tận dụng luôn Terminal của MacOS để kết nối đến VPS thông qua SSH cho nhanh.

Đầu tiên tại trang quản lý của Vultr chúng ta cần thêm key SSH. Key này do chúng ta tự tạo và quản lý

An SSH Key allows you to log into your server without needing a password. SSH Keys can be automatically added to servers during the installation process.

Creating an SSH key on Windows

The simplest way to create SSH key on Windows is to use PuTTYgen. Download and run PuTTYgen. Click the "Generate" button. For additional security, you can enter a key passphrase. This will be required to use the SSH key, and will prevent someone with access to your key file from using the key. Once the key has been generated, click "Save Private Key". Make sure you save this somewhere safe, as it is not possible to recover this file if it gets lost Select all of the text in the "Public key for pasting into OpenSSH authorized_keys file". This is what you would need to enter into the control panel to use the SSH key.

Creating an SSH key on Linux

The tools to create and use SSH are standard, and should be present on most Linux distributions. With the following commands, you can generate ssh key. Run: ssh-keygen -t rsa. For a more secure 4096-bit key, run: ssh-keygen -t rsa -b 4096 Press enter when asked where you want to save the key (this will use the default location). Enter a passphrase for your key. Run cat ~/.ssh/id_rsa.pub - this will give you the key in the proper format to paste into the control panel. Make sure you backup the ~/.ssh/id_rsa file. This cannot be recovered if it is lost.

Adding an SSH key to your control panel

Once you're logged in, go to https://my.vultr.com/sshkeys. Click "Add SSH Key". Enter a descriptive name for the key. Paste in your SSH public key. This is a long string beginning with "ssh-rsa". You should have saved this from when you generated your key. Click "Add SSH Key". Now, when you're deploying servers you will be able to select which SSH keys you want to add to the newly deployed server. Remember to select the keys before the initial server deployment, otherwise you will need to log into the newly created server and add the SSH keys manually.

Limitations

SSH keys are only available for Linux and FreeBSD. They are not supported for Windows, custom ISOs, nor snapshot restores. SSH keys can only be managed from the control panel during deployment. You cannot use the control panel to manage them on an already-installed instance.

Connecting to a server using an SSH key from a Windows client

Download and run the PuTTY SSH client. Type the IP address or Username + IP address ( [email protected] ) of the destination server under the "Host Name" field on the "Session" category. Navigate to the "Connection -> SSH -> Auth" category (left-hand side). Click "Browse..." near "Private key file for authentication". Choose the private key file (ending in .ppk) that you generated earlier with PuTTYgen. Click "Open" to initiate the connection. When finished, end your session by pressing Ctrl+d.

Connecting to a server using an SSH key from a Linux client

Check that your Linux operating system has an SSH client installed ( which ssh ). If a client is not installed, you will need to install one. Initiate a connection: ssh -i /path/to/id_rsa [email protected] When finished, end your session by pressing Ctrl+d.