# 创建安装目录(也可不创建,直接解压到/etc下) mkdir -p /etc/nginx # 下载 Nginx 源码包(请替换为最新版本) wget http://nginx.org/download/nginx-1.22.1.tar.gz # 解压源码包到 /etc/nginx tar -zxvf nginx-1.22.1.tar.gz -C /etc/ mv /etc/nginx-1.22.1 /etc/nginx
cd /etc/nginx/nginx # 配置编译选项 ./configure \ --prefix=/etc/nginx \ --sbin-path=/usr/sbin/nginx \ --conf-path=/etc/nginx/nginx.conf \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --pid-path=/var/run/nginx.pid \ --with-http_ssl_module # 编译并安装 make make install
编辑 /etc/nginx/nginx.conf 文件,以下是基本配置示例:
user nginx; worker_processes auto; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; location / { root /usr/share/nginx/html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } } }
创建 /etc/systemd/system/nginx.service 文件,内容如下:
[Unit] Description=The NGINX HTTP and reverse proxy server After=network.target remote-fs.target nss-lookup.target [Service] Type=forking PIDFile=/var/run/nginx.pid ExecStartPre=/usr/sbin/nginx -t ExecStart=/usr/sbin/nginx ExecReload=/usr/sbin/nginx -s reload ExecStop=/bin/sh -c "/usr/sbin/nginx -s quit" [Install] WantedBy=multi-user.target
# 重新加载 systemd 配置 systemctl daemon-reload <!-- more --> # 启动 Nginx systemctl start nginx # 设置开机自启 systemctl enable nginx # 查看 Nginx 状态 systemctl status nginx # 重启 Nginx systemctl restart nginx
本文作者:lihongzhen
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!