nginx源码安装
为某一server配置日志的话,要先打开主配置文件中的日志
模块的默认安装和直接调用
0.卸载yum安装的nginx
yum remove -y nginx
1.安装依赖包
[root@lb02 ~]# yum install -y gcc glibc gcc-c++ pcre-devel openssl-devel
2.下载nginx源码包
[root@lb02 ~]# wget http://nginx.org/download/nginx-1.16.1.tar.gz
3.解压nginx源码包以及第三方模块
[root@lb02 ~]# tar xf nginx-1.16.1.tar.gz
4.进入nginx目录
[root@lb02 ~]# cd nginx-1.16.1/
5.生成,只是生成一个Makefile可执行文件
./configure --prefix=/app/nginx-1.16.1 --user=www --group=www --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
6.编译
7.安装
make && make install
[root@lb01 /app/nginx-1.16.1]# ll
total 0
drwxr-xr-x 2 root root 333 May 29 00:02 conf
drwxr-xr-x 2 root root 40 May 29 00:02 html
drwxr-xr-x 2 root root 6 May 29 00:02 logs
drwxr-xr-x 2 root root 19 May 29 00:02 sbin #nginx启动程序
8.做软链接,
ln -s /app/nginx-1.16.1 /app/nginx
9.修改nginx.conf,创建相关目录
[root@lb01 ~]# mkdir /app/nginx/conf/conf.d
[root@lb01 ~]# vim /app/nginx/conf/nginx.conf
user www;
...
include /app/nginx/conf/conf.d/*.conf;
[root@lb01 ~]# groupadd www -g666 && useradd www -u 666 -g 666
10.手写server语句
vim /app/nginx/conf/conf.d/cs.conf
server {
listen 80;
server_name cs.ym.com;
location / {
root html/wp;
index index.html;
}
location = /zt {
stub_status;
}
}
11.创建站点目录
mkdir /app/nginx/html/wp/ -p
echo lb01 > /app/nginx/html/wp/index.html
12.添加环境变量
echo 'export PATH="/app/nginx/sbin:$PATH"' > /etc/profile.d/export.sh
source /etc/profile.d/export.sh
13.启动nginx
[root@lb01 ~]# nginx
[root@lb01 ~]# nginx -t
[root@lb01 ~]# nginx -sreload
#查看nginx是否重载
ps -ef|grep nginx
14.域名解析
10.0.0.5 cs.cs.com
nginx在不影响用户体验的情况下,增加模块,秒级升级,秒级回滚
logs/nginx.pid存放的是nginx的master进程的进程号。(该文件相当于一个变量,中间商)
0.使用wget下载指定版本的源码nginx
1.确认要增加的模块
nginx -V
./configure --prefix=/app/nginx-1.16.1_new --user=www --group=www --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module
2.进入安装包目录
cd /nginx-1.16.1
3.生成
./configure --prefix=/app/nginx-1.16.1_new --user=www --group=www --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-xxx
4.编译
5.安装
make && make install
6.覆盖相关文件,防止升级后数据丢失
拷贝nginx主配置文件
cp /app/nginx-1.16.1/conf/nginx.conf /app/nginx-1.16.1_new/conf/nginx.conf
拷贝nginx从配置文件
mkdir /app/nginx-1.16.1_new/conf/conf.d/
cp -a /app/nginx-1.16.1/conf/conf.d/* /app/nginx-1.16.1_new/conf/conf.d/
#拷贝pid,防止软链接后,因为pid的不同导致nginx状态异常
cp -a /app/nginx-1.16.1/logs/nginx.pid /app/nginx-1.16.1_new/logs/nginx.pid
拷贝站点目录
cp -a /app/nginx-1.16.1/html/wp/index.html/* /app/nginx-1.16.1_new/html/wp/index.html
7.秒级升级
rm -rf /app/nginx && ln -s /app/nginx-1.16.1_new /app/nginx && nginx -sreload
8.秒级回滚
rm -rf /app/nginx && ln -s /app/nginx-1.16.1 /app/nginx
9.查看nginx版本
nginx -v
实时监控 网站状态脚本
#!/bin/bash
while true;do
A=`curl -I -m 10 -o /dev/null -s -w %{http_code} http://cs.ym.com`
if [ $A -eq 200 ];then
echo '$(date %F-%T)_网站正常' >>/tmp/a.log
else
echo '$(date %F-%T)_网站炸了' >>/tmp/a.log
sleep 1
done