一键安装脚本
前言
最近在做学web相关的知识,经常需要新建虚拟机做实验,如果手工安装nginx就比较费时间了,下面是我写的一个一键安装nginx的脚本,基本每个命令都有写注释,大家使用前可以先全程看一遍进行再根据自己的需求进行改良,小白的话直接用我的配置也是可以的,只需保证服务器能上网即可。
将脚本中的 {?} 替换成其他名字(去掉大括号),用于做nginx文件夹的名字和启动nginx服务的用户的名字
mkdir -p /my_nginx
cd /my_nginx
# 下载nginx压缩包
curl -O http://nginx.org/download/nginx-1.23.3.tar.gz
# 或者用wget
#wget http://nginx.org/download/nginx-1.23.3.tar.gz
# 解压
tar xf nginx-1.23.3.tar.gz
# 进入文件夹
cd nginx-1.23.3
# 新建用户,用于启动nginx进程,名字自拟
useradd -s /sbin/nologin {?}
# 安装依赖包
# ssh相关、gcc为编译需要、pcre正则相关、make编译相关
yum install -y openssl openssl-devel gcc pcre pcre-devel automake make
# configure是一个配置的脚本文件,会根据指定的配置生成一个Makefile文件,这个文件会影响后面make命令的编译,相当于图纸
# configure可配置参数可以参考官方文档:http://nginx.org/en/docs/configure.html
# 常用选项:
# --with-*:开启某个功能,默认不安装 --without-*:禁用某个功能,默认安装
# --prefix=path:指定路径 --conf-path=path:指定配置文件路径,不指定会放到prefix路径下
# --user=name:指定启动nginx worker进程的用户
# --with-http_ssl_moudle 开启https的功能
# --without-http——memcached_moudle 禁用http_memcached
# --with-http_v2_module:对http2.0版本的支持
# --with-threads:开启线程池功能 --with-http_stub_status_moudle:开启nginx状态统计功能,可以知道多少访问
# --with-stream 支持tcp/udp反向代理
./configure --prefix=/usr/local/{?} --user={?} --with-http_ssl_module --with-http_v2_module --with-threads --with-http_stub_status_module --with-stream
# 编译
# make是按照Makefile的配置去编译程序为二进制文件,二进制文件就是执行可以运行的程序
# -j:指定编译进程数,多点速度快些,可以使用top后按1查看虚拟机配有几个核心
make -j2
# 将编译好的二进制文件复制到指定安装路径目录下
make install
# 启动nginx
/usr/local/{?}/sbin/nginx
# 修改PATH变量
PATH=$PATH:/usr/local/{?}/sbin
echo "PATH=$PATH" >>/root/.bashrc
# 设置nginx的开机启动,rc.local是指向rc.d/rc.local的链接,后者需要拥有执行权限才能开机自启
echo "/usr/local/{?}/sbin/nginx" >>/etc/rc.local
chmod +x /etc/rc.d/rc.local
# selinux和firewalld防火墙都关闭
# selinux临时和永久关闭
setenforce 0
sed -i '/^SELINUX=/ s/enforcing/disabled/' /etc/selinux/config
# 防火墙临时和永久关闭
service firewalld stop
systemctl disable firewalld
执行完脚本后查看nginx进程是否正常启动:
[root@localhost sbin]$ ps aux |grep nginx # 显示有master和worker进程即为正常
root 24084 0.0 0.0 46236 1168 ? Ss 20:51 0:00 nginx: master process nginx
zh54 24085 0.0 0.1 46696 1924 ? S 20:51 0:00 nginx: worker process
root 24131 0.0 0.0 112824 988 pts/0 S+ 21:00 0:00 grep --color=auto nginx
[root@localhost sbin]$ netstat -anplut |grep nginx # 没有该命令的话先执行yum install net-tools -y
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 24084/nginx: master
查看selinux和firewalld是否都已经关闭:
[root@localhost sbin]$ getenforce
Permissive # 显示宽容的状态即可
[root@localhost sbin]$ systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead) # 显示dead即可
Docs: man:firewalld(1)
3月 26 14:55:34 localhost.localdomain systemd[1]: Starting firewalld - dynamic firewall daemon...
3月 26 14:55:35 localhost.localdomain systemd[1]: Started firewalld - dynamic firewall daemon.
3月 26 20:38:04 localhost.localdomain systemd[1]: Stopping firewalld - dynamic firewall daemon...
3月 26 20:38:05 localhost.localdomain systemd[1]: Stopped firewalld - dynamic firewall daemon.
下面是一些简单的命令
常用命令
命令可参考官方链接:初学者指南 (nginx.org)
nginx:启动
nginx -s:传递参数
nginx -s stop:快速关机
nginx -s quit:优雅关机
nginx -s reload:重新加载配置文件,开启新进程的同时保持旧进程,新用户进新进程,旧用户离开再关闭旧进程,全程业务不停
nginx -v:查看版本
nginx -t:测试配置文件是否出错
小结
文章就到这里,脚本有问题的话欢迎指出,作者会及时更正,谢谢观看!