Nginx
编译安装
安装所需环境
yum install -y gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel wget
以下两种下载方式 自己选择
官网下载nginx二进制包
官网下载地址 https://nginx.org/download/
wget下载nginx
wget -c https://nginx.org/download/nginx-1.12.0.tar.gz
解压
tar -zxvf nginx-1.12.0.tar.gz
cd nginx-1.12.0
配置
创建临时目录
mkdir -p /var/temp/nginx/client /var/temp/nginx/proxy /var/temp/nginx/fastcgi /var/temp/nginx/uwsgi /var/temp/nginx/scgi
./configure \
--prefix=/usr/local/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--pid-path=/usr/local/nginx/conf/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-pcre \
--with-file-aio \
--with-http_realip_module \
--without-http_scgi_module \
--without-http_uwsgi_module \
--without-http_fastcgi_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi
Nginx 1.9.0及以后的版本可以加上stream 模块
--with-stream
stream 模块例子
stream与http平级
stream {
upstream wg {
server 10.158.242.144:8183 weight=1 max_fails=3 fail_timeout=15s;
server 10.158.242.145:8183 weight=1 max_fails=3 fail_timeout=15s;
}
server {
listen 8183;
proxy_pass wg;
}
}
回显如下
编译 安装
make && make install
回显如下
启动
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
重载
/usr/local/nginx/sbin/nginx -s reload
停止
/usr/local/nginx/sbin/nginx -s stop
查看版本 安装的模块
/usr/local/nginx/sbin/nginx -V
如果认为这种启动方式麻烦 可以创建一个systemd的nginx服务文件
创建文件
vim /lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/conf/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
按键盘Esc :wq 保存下
赋权
chmod 754 /lib/systemd/system/nginx.service
启动
systemctl start nginx
设置开机自启
systemctl enable nginx.service
yum安装
添加yum源
vim /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
pgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
按键盘Esc :wq 保存下
查询一下yum源配置
yum list |grep nginx
回显如下
安装Nginx
yum list nginx --showduplicates
安装指定的版本
yum -y install nginx-1.18.0-2.el7.ngx
查看nginx安装包信息 或者 nginx版本
rpm -qi nginx 或者 nginx -v
启动停止重载重启Nginx
systemctl start nginx
systemctl stop nginx
systemctl restart nginx
systemctl status nginx
systemctl reload nginx
yum安装Nginx配置信息默认目录
/usr/share/nginx/html 网站文件默认存放目录
/etc/nginx/conf.d/default.conf 默认站点配置
/etc/nginx/conf.d/ 自定义nginx站点配置文件目录
/etc/nginx/nginx.conf 全局配置