跳轉到內容
QQ群

安裝Nginx、更改主機名

精選回覆

發表於

安裝Nginx、更改主機名

---------------
安裝Nginx
---------------

apt update


apt upgrade -y


apt dist-upgrade -y


apt install zip unzip



apt install curl vim wget gnupg dpkg apt-transport-https lsb-release ca-certificates




增加 https://n.wtf GPG Key
curl -sSL https://n.wtf/public.key | gpg --dearmor > /usr/share/keyrings/n.wtf.gpg



加入 https://n.wtf/ 的Nginx源
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/n.wtf.gpg] https://mirror-cdn.xtom.com/sb/nginx/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/n.wtf.list




更新
apt update


安裝nginx
apt install nginx-extras -y




瀏覽nginx版本,顯示 nginx version: nginx-n.wtf/1.27.4
nginx -V




----------------------------
更改主機名
----------------------------

更改主機名

hostnamectl set-hostname 域名.com



重開機
reboot



SSH登入,將顯示 Linux 你的域名.com 6.1.0-32-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.129-1 (2025-03-06) x86_64




----------------------------
配置Nginx
----------------------------


預設網站目錄
mkdir -p /var/www/域名.com



新增 域名.com.conf
vi /etc/nginx/sites-available/域名.com.conf


----------------------------------------------

server {
        listen 80;

        root /var/www/域名.com;
        index index.html index.htm index.php;

        server_name 域名.com;

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

----------------------------------------------

ESC儲存並離開
:wq




軟連結到 /etc/nginx/sites-enabled 目錄,讓它生效
ln -s /etc/nginx/sites-available/域名.com.conf /etc/nginx/sites-enabled/域名.com.conf



重新載入Nginx
nginx -t


nginx -s reload






在網站目錄中,新增一個index.html
vi /var/www/域名.com/index.html



在此檔案中,複製貼上
Just test



ESC儲存並離開
:wq



----------------------------------
開機啟動 Nginx
----------------------------------

開機啟動
systemctl enable nginx


啟動
systemctl start nginx


重開機
reboot




瀏覽nginx狀態
systemctl status nginx

按 ctrl + z 停止



這時,瀏覽器可進入你的網站

http://域名.com/

建立帳號或登入後發表意見