(Linux系统之Nginx的编译安装)
一、检查系统版本
[root@server001 ~]# cat /etc/os-release
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"
CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"
二、编译环境配置
1.安装编译依赖包
yum -y groupinstall Development tools
yum -y install pcre pcre-devel zlib zlib-devel openssl openssl-devel wget
2.创建nginx用户
useradd nginx -s /sbin/nologin -M
三、下载nginx源码包
wget http://nginx.org/download/nginx-1.9.9.tar.gz
四、编译安装nginx
1.解压源码包
[root@server001 nginx]# tar -xzf nginx-1.9.9.tar.gz
[root@server001 nginx]# ls
index.html nginx-1.9.9 nginx-1.9.9.tar.gz
[root@server001 nginx]#
2.进入软件目录
[root@server001 nginx]# cd nginx-1.9.9/
[root@server001 nginx-1.9.9]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
[root@server001 nginx-1.9.9]#
3.开始编译安装nginx
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module
make
make install
[root@server001 nginx-1.9.9]# make install
make -f objs/Makefile install
make[1]: Entering directory `/data/nginx/nginx-1.9.9'
test -d '/usr/local/nginx' || mkdir -p '/usr/local/nginx'
test -d '/usr/local/nginx/sbin' || mkdir -p '/usr/local/nginx/sbin'
test ! -f '/usr/local/nginx/sbin/nginx' || mv '/usr/local/nginx/sbin/nginx' '/usr/local/nginx/sbin/nginx.old'
cp objs/nginx '/usr/local/nginx/sbin/nginx'
test -d '/usr/local/nginx/conf' || mkdir -p '/usr/local/nginx/conf'
cp conf/koi-win '/usr/local/nginx/conf'
cp conf/koi-utf '/usr/local/nginx/conf'
cp conf/win-utf '/usr/local/nginx/conf'
test -f '/usr/local/nginx/conf/mime.types' || cp conf/mime.types '/usr/local/nginx/conf'
cp conf/mime.types '/usr/local/nginx/conf/mime.types.default'
test -f '/usr/local/nginx/conf/fastcgi_params' || cp conf/fastcgi_params '/usr/local/nginx/conf'
cp conf/fastcgi_params '/usr/local/nginx/conf/fastcgi_params.default'
test -f '/usr/local/nginx/conf/fastcgi.conf' || cp conf/fastcgi.conf '/usr/local/nginx/conf'
cp conf/fastcgi.conf '/usr/local/nginx/conf/fastcgi.conf.default'
test -f '/usr/local/nginx/conf/uwsgi_params' || cp conf/uwsgi_params '/usr/local/nginx/conf'
cp conf/uwsgi_params '/usr/local/nginx/conf/uwsgi_params.default'
test -f '/usr/local/nginx/conf/scgi_params' || cp conf/scgi_params '/usr/local/nginx/conf'
cp conf/scgi_params '/usr/local/nginx/conf/scgi_params.default'
test -f '/usr/local/nginx/conf/nginx.conf' || cp conf/nginx.conf '/usr/local/nginx/conf/nginx.conf'
cp conf/nginx.conf '/usr/local/nginx/conf/nginx.conf.default'
test -d '/usr/local/nginx/logs' || mkdir -p '/usr/local/nginx/logs'
test -d '/usr/local/nginx/logs' || mkdir -p '/usr/local/nginx/logs'
test -d '/usr/local/nginx/html' || cp -R html '/usr/local/nginx'
test -d '/usr/local/nginx/logs' || mkdir -p '/usr/local/nginx/logs'
make[1]: Leaving directory `/data/nginx/nginx-1.9.9'
[root@server001 nginx-1.9.9]#
4.检查nginx配置文件
[root@server001 local]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
5.复制nginx文件
cp -a /usr/local/nginx/sbin/nginx /usr/bin/
6.查看nginx命令帮助
[root@server001 local]# nginx -h
nginx version: nginx/1.9.9
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]
Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit
-t : test configuration and exit
-T : test configuration, dump it and exit
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload
-p prefix : set prefix path (default: /usr/local/nginx/)
-c filename : set configuration file (default: conf/nginx.conf)
-g directives : set global directives out of configuration file
7.查看nginx版本
[root@server001 local]# nginx -v
nginx version: nginx/1.9.9
五、启动nginx服务
1.启动nginx
[root@server001 local]# nginx
[root@server001 local]#
2.检查80端口
[root@server001 local]# netstat -tunlp |grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 78975/nginx: master
3.测试服务
[root@server001 nginx]# ls
index.html nginx-1.9.9 nginx-1.9.9.tar.gz
[root@server001 nginx]# cat index.html
nginx aa-bb
[root@server001 nginx]# cp index.html /usr/local/nginx/html/
cp: overwrite ‘/usr/local/nginx/html/index.html’? yes
[root@server001 nginx]# curl 192.168.3.166
nginx aa-bb
六、将nginx配置service管理
1.编辑nginx.service
[root@server001 nginx]# cat /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[root@server001 nginx]#
2.重启服务
[root@server001 nginx]# systemctl daemon-reload
[root@server001 nginx]# systemctl start nginx
3.查看nginx状态
[root@server001 nginx]# systemctl status nginx
● nginx.service - nginx
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: active (running) since Fri 2022-11-18 16:49:02 CST; 6s ago
Process: 79558 ExecStart=/usr/local/nginx/sbin/nginx (code=exited, status=0/SUCCESS)
Main PID: 79559 (nginx)
Tasks: 2
Memory: 1.4M
CGroup: /system.slice/nginx.service
├─79559 nginx: master process /usr/local/nginx/sbin/nginx
└─79560 nginx: worker process
Nov 18 16:49:02 server001 systemd[1]: Starting nginx...
Nov 18 16:49:02 server001 systemd[1]: Started nginx.