Ubuntu编译安装Nginx(转)
1、依赖安装
sudo apt-get update sudo apt-get install build-essential sudo apt-get install libtool sudo apt-get install libpcre3 libpcre3-dev sudo apt-get install zlib1g-dev sudo apt-get install libssl-dev
2、nginx源码包 (也可自行去 http://nginx.org/download/ 找想要的包)
wget http://nginx.org/download/nginx-1.21.6.tar.gz tar -zxvf nginx-1.21.6.tar.gz cd ./nginx-1.21.6
3、增加nginx用户(编译参数中 指定了以名为nginx的用户运行,所以先增加一个用户,不然会报错)
groupadd nginx useradd -M -s /sbin/nologin -g nginx nginx
4、编译安装
./configure --prefix=/usr/local/nginx \ --user=nginx --group=nginx \ --with-http_gzip_static_module \ --with-http_flv_module \ --with-http_ssl_module \ --with-http_realip_module \ --with-http_v2_module \ --with-http_sub_module \ --with-http_mp4_module \ --with-http_stub_status_module \ --with-http_gzip_static_module \ --with-pcre --with-stream \ --with-stream_ssl_module \ --with-stream_realip_module make && make install
5、创建服务文件
#创建服务文件 vim /lib/systemd/system/nginx.service #写入以下内容 [Unit] Description=The NGINX HTTP and reverse proxy server After=syslog.target network-online.target remote-fs.target nss-lookup.target Wants=network-online.target [Service] Type=forking PIDFile=/usr/local/nginx/logs/nginx.pid ExecStartPre=/usr/local/nginx/sbin/nginx -t ExecStart=/usr/local/nginx/sbin/nginx ExecReload=/usr/local/nginx/sbin/nginx -s reload ExecStop=/bin/kill -s QUIT $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target
6、加入开机启动
systemctl enable nginx
7、基本操作
#创建软链接 ln -s /usr/local/nginx/sbin/nginx /usr/bin/ #测试配置 nginx -t #以默认配置运行 nginx 或 systemctl start nginx #平滑重启 nginx -s reload #重启服务 systemctl restart nginx #停止服务 systemctl stop nginx