Linux系统之Nginx的编译安装

简介: 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

image.png

四、编译安装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.

相关文章
|
1月前
|
Ubuntu Linux Anolis
Linux系统禁用swap
本文介绍了在新版本Linux系统(如Ubuntu 20.04+、CentOS Stream、openEuler等)中禁用swap的两种方法。传统通过注释/etc/fstab中swap行的方式已失效,现需使用systemd管理swap.target服务或在/etc/fstab中添加noauto参数实现禁用。方法1通过屏蔽swap.target适用于新版系统,方法2通过修改fstab挂载选项更通用,兼容所有系统。
136 3
Linux系统禁用swap
|
1月前
|
Linux
Linux系统修改网卡名为eth0、eth1
在Linux系统中,可通过修改GRUB配置和创建Udev规则或使用systemd链接文件,将网卡名改为`eth0`、`eth1`等传统命名方式,适用于多种发行版并支持多网卡配置。
184 3
|
Ubuntu Linux 网络安全
Linux系统初始化脚本
一款支持Rocky、CentOS、Ubuntu、Debian、openEuler等主流Linux发行版的系统初始化Shell脚本,涵盖网络配置、主机名设置、镜像源更换、安全加固等多项功能,适配单/双网卡环境,支持UEFI引导,提供多版本下载与持续更新。
155 0
Linux系统初始化脚本
|
2月前
|
运维 Linux 开发者
Linux系统中使用Python的ping3库进行网络连通性测试
以上步骤展示了如何利用 Python 的 `ping3` 库来检测网络连通性,并且提供了基本错误处理方法以确保程序能够优雅地处理各种意外情形。通过简洁明快、易读易懂、实操性强等特点使得该方法非常适合开发者或系统管理员快速集成至自动化工具链之内进行日常运维任务之需求满足。
153 18
|
1月前
|
安全 Linux Shell
Linux系统提权方式全面总结:从基础到高级攻防技术
本文全面总结Linux系统提权技术,涵盖权限体系、配置错误、漏洞利用、密码攻击等方法,帮助安全研究人员掌握攻防技术,提升系统防护能力。
150 1
|
1月前
|
监控 安全 Linux
Linux系统提权之计划任务(Cron Jobs)提权
在Linux系统中,计划任务(Cron Jobs)常用于定时执行脚本或命令。若配置不当,攻击者可利用其提权至root权限。常见漏洞包括可写的Cron脚本、目录、通配符注入及PATH变量劫持。攻击者通过修改脚本、创建恶意任务或注入命令实现提权。系统管理员应遵循最小权限原则、使用绝对路径、避免通配符、设置安全PATH并定期审计,以防范此类攻击。
616 1
|
2月前
|
缓存 监控 Linux
Linux系统清理缓存(buff/cache)的有效方法。
总结而言,在大多数情形下你不必担心Linux中buffer与cache占用过多内存在影响到其他程序运行;因为当程序请求更多内存在没有足够可用资源时,Linux会自行调整其占有量。只有当你明确知道当前环境与需求并希望立即回收这部分资源给即将运行重负载任务之前才考虑上述方法去主动干预。
970 10
|
27天前
|
Linux 应用服务中间件 Shell
二、Linux文本处理与文件操作核心命令
熟悉了Linux的基本“行走”后,就该拿起真正的“工具”干活了。用grep这个“放大镜”在文件里搜索内容,用find这个“探测器”在系统中寻找文件,再用tar把东西打包带走。最关键的是要学会使用管道符|,它像一条流水线,能把这些命令串联起来,让简单工具组合出强大的功能,比如 ps -ef | grep 'nginx' 就能快速找出nginx进程。
271 1
二、Linux文本处理与文件操作核心命令
|
27天前
|
安全 Ubuntu Unix
一、初识 Linux 与基本命令
玩转Linux命令行,就像探索一座新城市。首先要熟悉它的“地图”,也就是/根目录下/etc(放配置)、/home(住家)这些核心区域。然后掌握几个“生存口令”:用ls看周围,cd去别处,mkdir建新房,cp/mv搬东西,再用cat或tail看文件内容。最后,别忘了随时按Tab键,它能帮你自动补全命令和路径,是提高效率的第一神器。
543 56