轻量级nginx,源码编译2进制安装

环境:

系统:redhat6.4

IP:193.168.20

nginx版本:nginx-1.18.0.tar.gz

安装方式:源码编译安装

安装:

安装ngin前,确保系统安装了软件库gcc,openssl-devel,pcre-devel,zlib-devel(用于编译安装依赖)

在rehat6.4及redhat7以上版本,系统光盘里有这些库,挂载安装就好

nginx下载地址:http://nginx.org/download/nginx-1.18.0.tar.gz

1.安装nginx

[root@redhat6 ~]# groupadd -r nginx
[root@redhat6 ~]# useradd -r -g nginx -s /bin/false -M nginx

[root@redhat6 ~]# tar xzf nginx-1.18.0.tar.gz 
[root@redhat6 ~]# cd nginx-1.18.0
[root@redhat6 nginx-1.18.0]# ./configure \
--prefix=/opt/nginx \
--conf-path=/opt/nginx/conf/nginx.conf \
--sbin-path=/opt/nginx/sbin/nginx \
--error-log-path=/opt/nginx/logs/error.log \
--http-log-path=/opt/nginx/logs/access.log \
--pid-path=/opt/nginx/run/nginx.pid \
--lock-path=/opt/nginx/run/nginx.lock \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--http-client-body-temp-path=/opt/nginx/client/ \
--http-proxy-temp-path=/opt/nginx/proxy/ \
--http-fastcgi-temp-path=/opt/nginx/fcgi/ \
--http-uwsgi-temp-path=/opt/nginx/uwsgi \
--http-scgi-temp-path=/opt/nginx/scgi \
--with-pcre
[root@redhat6 nginx-1.18.0]# make && make install

  2.启动:

[root@redhat6 ~]# yum install -y net-tools (查看端口状态工具)
[root@redhat6 ~]# netstat -ntpl |grep 80 (检查80端口是否占用)
[root@redhat6 ~]# ps -ef |grep nginx     (检查nginx是否以启动)
root       4729   4222  0 07:21 pts/2    00:00:00 grep nginx
[root@redhat6 ~]# /opt/nginx/sbin/nginx
[root@redhat6 ~]# netstat -ntpl |grep 80
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      4734/nginx          
[root@redhat6 ~]# ps -ef |grep nginx
root       4734      1  0 07:22 ?        00:00:00 nginx: master process /apt/nginx/sbin/nginx
nginx      4735   4734  0 07:22 ?        00:00:00 nginx: worker process
root       4739   4194  0 07:23 pts/1    00:00:00 grep nginx
3.访问:
[root@redhat6 ~]# iptables -F (不关防火墙,就要配置允许nignx,否则不可访问)
[root@redhat6 ~]# curl http://193.168.0.20 (下图是windows上的访问例子)

 

 

(注:停止&重读配置文件)

[root@redhat6 ~]# /apt/nginx/sbin/nginx -s stop      #停止服务
[root@redhat6 ~]# /apt/nginx/sbin/nginx -s reload  #重载服务配置
出现重载错误问题:

[root@redhat ~]# /opt/nginx/sbin/nginx -s reload
nginx: [error] open() "/opt/nginx/run/nginx.pid" failed (2: No such file or directory)

  使用nginx -c 指定配置文件:/opt/nginx/sbin/nginx -c /opt/nginx/conf/nginx.conf

4.怎么添加到systemctl(redhat7以上才支持):

编译安装的nginx需要添加rc.local

nginx.service要添加可执行权限

加入前要先停止nginx,在用systemctl启动,不然会报错,显示nginx已在运行无法重启进程

[root@redhat ~]# cd /usr/lib/systemd/system
[root@redhat system]# vim nginx.service 
[Unit]
Description=nginx
After=network.target

[Service]
Type=forking
ExecStart=/opt/nginx/sbin/nginx -c /opt/nginx/conf/nginx.conf
ExecReload=/opt/nginx/sbin/nginx -c /opt/nginx/conf/nginx.conf
ExecStop=/opt/nginx/sbin/nginx -s stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target
[root@redhat system]# chmod +x nginx.service
[root@redhat system]# /opt/nginx/sbin/nginx -s stop
[root@redhat system]# systemctl daemon-reload 

  

安装路径要确定,一般其他日志这些都默认安装到“安装路径”子目录下面

--prefix=/opt/nginx (安装路径)
--conf-path=/opt/nginx/conf/nginx.conf (主配置文件路径)
--sbin-path=/opt/nginx/sbin/nginx (2进制进程启动路径)
--error-log-path=/opt/nginx/logs/error.log (日志)
--http-log-path=/opt/nginx/logs/access.log (日志)
--pid-path=/opt/nginx/run/nginx.pid (进程pid路径名)
--lock-path=/opt/nginx/run/nginx.lock
--user=nginx (运行用户)
--group=nginx (运行组)
--with-http_ssl_module (启用ssl模块)
--with-http_flv_module
--with-http_stub_status_module (启用监控状态)
--with-http_gzip_static_module
--http-client-body-temp-path=/opt/nginx/client/
--http-proxy-temp-path=/opt/nginx/proxy/
--http-fastcgi-temp-path=/opt/nginx/fcgi/
--http-uwsgi-temp-path=/opt/nginx/uwsgi
--http-scgi-temp-path=/opt/nginx/scgi
--with-pcre

常用命令:

开启nginx /usr/local/nginx/sbin/nginx
关闭nginx /usr/local/nginx/sbin/nginx -s stop
重启nginx /usr/local/nginx/sbin/nginx -s reload
测试配置和退出 /usr/local/nginx/sbin/nginx -t
nginx配置文件

/usr/local/nginx/conf/nginx.conf

如有什么细节,还请大家指出

 

posted @ 2021-03-17 11:07  道观猫  阅读(171)  评论(0编辑  收藏  举报