1. 安装依赖项
bash
sudo apt update sudo apt install -y build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev
2. 创建系统用户tom
bash
sudo useradd -s /sbin/nologin tom
3. 下载并编译Nginx 1.22.1
bash
wget http://nginx.org/download/nginx-1.22.1.tar.gz tar -zxvf nginx-1.22.1.tar.gz cd nginx-1.22.1 ./configure --user=tom --group=tom --prefix=/usr/local/nginx --with-http_ssl_module make sudo make install
4. 创建网站目录和内容
bash
sudo mkdir -p /var/www/test sudo chown tom:tom /var/www/test echo "I AM TEST WEB HTML" | sudo tee /var/www/test/index.html
5. 配置HTTP基本认证
bash
sudo mkdir -p /etc/nginx sudo htpasswd -c /etc/nginx/.htpasswd tom # 按提示设置密码(需输入两次) sudo chmod 640 /etc/nginx/.htpasswd sudo chown root:tom /etc/nginx/.htpasswd
6. 配置Nginx虚拟主机
编辑/usr/local/nginx/conf/nginx.conf
,