计算机网络/计算机科学与应用/系统/运维/开发

8、Nginx 安装操作


官网地址:nginx.org

安装方式:源代码安装

cd  /etc/yum.repos.d/


步骤:

1、备份旧的yum源

mkdir repobak
cd repobak
mv ../*  .


以上操作就是使得yum仓库一层目录,开始备份,可以访问阿里云的镜像站,重新部署yum源


2、安装nginx依赖包

# yum install -y gcc gcc-c++ autoconf automake make
# yum install zlib zlib-devel openssl openssl-devel pcre  pcre-devel wget httpd-tools  vim -y


下载源代码

# wget http://nginx.org/download/nginx-1.17.10.tar.gz


还可以获取淘宝的nginx代码

# wget http://tengine.taobao.org/download/tengine-2.3.3.tar.gz


解压缩淘宝Nginx

# tar -zxvf tengine-2.3.3.tar.gz


进入源代码目录下,准备开始编译安装

# cd tengine-2.3.3
# pwd
# ls
# ./configure  --help 查看帮助文档


执行编译,选择编译模块

# ./configure  \
--prefix=/usr/local/nginx  \
--with-http_ssl_module  \
--with-http_flv_module  \
--with-http_gzip_static_module  \
--with-http_stub_status_module \
--with-threads  \
--with-file-aio \
--add-module=/root/nginx-accesskey-2.0.3


./configure --help查看安装的所需模块

[root@node-a nginx-1.23.3]# ./configure --help
  --help                             print this message
  --prefix=PATH                      set installation prefix
  --sbin-path=PATH                   set nginx binary pathname
  --modules-path=PATH                set modules path
  --conf-path=PATH                   set nginx.conf pathname
  --error-log-path=PATH              set error log pathname
  --pid-path=PATH                    set nginx.pid pathname
  --lock-path=PATH                   set nginx.lock pathname
  --user=USER                        set non-privileged user for
                                     worker processes
  --group=GROUP                      set non-privileged group for
                                     worker processes
  --build=NAME                       set build name
  --builddir=DIR                     set build directory

--with-xxx 表示启用,--without-xxx 表示禁用


如果需要添加第三方模块,使用--add-module

--add-module=/root/nginx-accesskey-2.0.3


编译安装

# make
# make install


关闭防火墙

systemctl stop firewalld


查看端口号

[root@VM-4-2-centos nginx]# netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:6379            0.0.0.0:*               LISTEN      19771/redis-server
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      7730/nginx: worker
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      2045/sshd
tcp        0      0 0.0.0.0:88              0.0.0.0:*               LISTEN      7730/nginx: worker
tcp6       0      0 :::21                   :::*                    LISTEN      10717/vsftpd
udp        0      0 0.0.0.0:68              0.0.0.0:*                           924/dhclient
udp        0      0 10.0.4.2:123            0.0.0.0:*                           654/ntpd
udp        0      0 127.0.0.1:123           0.0.0.0:*                           654/ntpd
udp6       0      0 fe80::5054:ff:fe37::123 :::*                                654/ntpd
udp6       0      0 ::1:123                 :::*                                654/ntpd


[root@VM-4-2-centos nginx]# ps -ef|grep nginx|grep -v grep
nobody    7730 11253  0 Jun26 ?        00:00:01 nginx: worker process
root     11253     1  0 Jun26 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf


访问地址:

http://42.192.180.238/


停止Nginx

[root@VM-4-2-centos sbin]# pwd
/usr/local/nginx/sbin
[root@VM-4-2-centos sbin]# ./nginx -s stop


配置path变量

# vim /etc/profile
或者
# vim /etc/profile.d/nginx.sh
export PATH="$PATH:/usr/local/nginx/sbin"

退出当前会话,重新登录,系统默认加载/etc/profile.d下所有的环境变量文件


查看path变量

[root@VM-4-2-centos sbin]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/mysql/bin:/root/bin


退出系统

# exit


重新查看环境变量

[root@VM-4-2-centos ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/nginx/sbin/:/usr/local/mysql/bin:/root/bin


然后就可以全局使用nginx环境了

[root@VM-4-2-centos ~]# nginx -s stop



查看80端口监听

[root@VM-4-2-centos ~]# netstat -tunlp|grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      9686/nginx: master
udp6       0      0 fe80::5054:ff:fe37::123 :::*                                654/ntpd


如果再次启动Nginx 则报错

[root@VM-4-2-centos ~]# nginx
nginx: [emerg] bind() to 0.0.0.0:88 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:88 failed (98: Address already in use)



可以停止nginx后,再次启动nginx服务,用于重新加载nginx配置

# nginx -s stop


如果不想重启nginx,直接重新加载配置文件,nginx 提供了一个reload功能,可以实现重新加载读取配置文件

# nginx -s reload


检查nginx编译信息

[root@VM-4-2-centos ~]# nginx -V
nginx version: nginx/1.21.6
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
configure arguments: --prefix=/usr/local/nginx


学会在学习中寻找乐趣,学会乐在其中并保持热情

评论

^