安装nginx
程序员文章站
2024-02-01 18:09:58
下载Nginx wget http://nginx.org/download/nginx-1.12.2.tar.gz 一、安装nginx时必须先安装相应的编译工具 [root@xuegod63 ~]#yum -y install gcc gcc-c++ autoconf automake gcc c ......
下载nginx
wget
一、安装nginx时必须先安装相应的编译工具
[root@xuegod63 ~]#yum -y install gcc gcc-c++ autoconf automake
gcc c语言编译器
gcc-c++ c++语言编译器
autoconf automake 用于make编译的工具
[root@xuegod63 ~]#yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel
zlib :nginx提供gzip模块,需要zlib库支持
openssl :nginx提供ssl功能
pcre :支持地址重写rewrite功能
安装nginx:
上传nginx软件包对
[root@xuegod63 ~]# yum install lrzsz -y
[root@xuegod63 ~]# tar zxvf nginx-1.12.2.tar.gz
[root@xuegod63 ~]# cd nginx-1.12.2
[root@xuegod63 nginx-1.12.2]# useradd -s /sbin/nologin -m nginx
#创建一个nginx用户
-s 指定登录shell
-m 不创建家目录
#groupadd -r nginx -r 系统账号
#useradd -g nginx -r nginx
#
[root@xuegod63 nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --sbin-path=/usr/bin/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-pcre --with-http_stub_status_module --with-http_addition_module --with-http_gzip_static_module #检查编译环境
参数说明:
--prefix=/usr/local/nginx 指定安装路径
--user=nginx --group=nginx 指定运行nginx进程的用户和组
--with-http_ssl_module 支持ssl加密
--with-http_realip_module 此模块支持显示真实来源ip地址,主要用于nginx做前端负载均衡服务器使用
--with-http_gzip_static_module 这个模块指定压缩
--with-pcre 此模块支持rewrite功能
--with-http_stub_status_module 此模块用于查看nginx的一些状态信息.
[root@xuegod63 nginx-1.12.2]# echo $?
0
编译和安装:
[root@xuegod63 nginx-1.12.2]# make -j 4 #编译,把源代码编译成可执行的二进制文件。
-j 4 #以4个进程同时编译
编译:mysql 内核 1个小时。 -j 4 20分钟。
[root@xuegod63 nginx-1.12.2]# make install #安装
nginx主要目录结构:
[root@xuegod63 nginx-1.12.2]# ls /usr/local/nginx/
conf html logs sbin
conf #配置文件
html #网站根目录
logs #日志
sbin #nginx启动脚本
启动nginx :
[root@xuegod63 ~]# nginx
查看版本
[root@xuegod63 ~]# nginx -v
查看安装具体信息
[root@xuegod63 ~]# nginx -v
需要重新进行nginx安装:
[root@xuegod63 nginx-1.12.2]# /usr/local/nginx/sbin/nginx -s stop
[root@xuegod63 nginx-1.12.2]# rm -rf /usr/local/nginx/
[root@xuegod63 nginx-1.12.2]# pwd
/root/nginx-1.10.2
[root@xuegod63 nginx-1.12.2]# make clean
rm -rf makefile objs
编写nginx的systemd启动脚本
systemd启动脚本不需要编写shell,只需在 /usr/lib/systemd/system/ 目录下建一个 nginx.service 脚本,内容如下
[root@xuegod63 nginx-1.12.2]# pwd
/usr/lib/systemd/system
[root@purenlai system]# cat nginx.service
[unit]
description=nginx - high performance web server
documentation=
#after参数设置项用来确认启动顺序
after=network-online.target remote-fs.target nss-lookup.target
wants=network-online.target
[service]
type=forking
pidfile=/usr/local/nginx/logs/nginx.pid
execstartpre=/usr/bin/nginx -t
execstart=/usr/bin/nginx -c /usr/local/nginx/conf/nginx.conf
execreload=/bin/kill -s hup $mainpid
execstop=/bin/kill -s term $mainpid
[install]
wantedby=multi-user.target
重新载入所有的[unit]文件
[root@purenlai system]# systemctl daemon-reload
[root@purenlai system]# systemctl start nginx
[root@purenlai system]# systemctl status nginx
● nginx.service - nginx - high performance web server
loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
active: active (running) since tue 2019-08-20 14:14:25 cst; 2h 50min ago
docs:
process: 25287 execstart=/usr/bin/nginx -c /usr/local/nginx/conf/nginx.conf (code=exited, status=0/success)
process: 25286 execstartpre=/usr/bin/nginx -t (code=exited, status=0/success)
main pid: 25290 (nginx)
cgroup: /system.slice/nginx.service
├─25290 nginx: master process /usr/bin/nginx -c /usr/local/nginx/conf/nginx.conf
└─25291 nginx: worker process
aug 20 14:14:25 purenlai systemd[1]: starting nginx - high performance web server...
aug 20 14:14:25 purenlai nginx[25286]: nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
aug 20 14:14:25 purenlai nginx[25286]: nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
aug 20 14:14:25 purenlai systemd[1]: failed to read pid from file /usr/local/nginx/logs/nginx.pid: invalid argument
aug 20 14:14:25 purenlai systemd[1]: started nginx - high performance web server.
|
推荐阅读