Skip to content

通过知识获得解放,通过技术获得自由

Menu
  • 专题目录
  • 液压相关
    • 液压技术
    • 液压相邻技术
    • 液压应用
  • 计算机相关
    • 计算机和软件
    • 网络和网站技术
  • 哲学
  • 关于本站
Menu

源码安装nginx1.18[0]

Posted on 2023年11月14日 by

在常规的x86平台,采用常见的linux发行版centos7系统,安装nginx的方式有三种,rpm软件包安装,源码安装,docker安装。从部署上的难度,docker最为简单(别人的容器直接下载和配置即可),其次为rpm软件包,源码装最难。

docker安装方式见docker专题,rpm安装方式见手动部署nginx——软件包。本篇将继续验证源码安装的过程。

nginx由于使用人数较多,因此对于此代码的验证和反馈也最多,为了减少对于维护团队的询问,此类代码一般有完整的说明文档和教程,并且在部署的过程中给出出错的原因用于debug和反馈。

采用源码安装的软件,能够在编译时检查软件运行需要的条件以及其他不正确的配置,能够将软件的环境错误在使用前识别出来,更加容易找到bug。对于能看懂源码的linux系统管理员,能够定制软件,实现差别化和自动化部署,能够实现跨linux发行版的部署。

采用源码安装nginx时候,首先需要部署基础编译环境。

以下是在完成编译环境下的,nginx源码安装过程。需要事先关闭selinux。

  • 前提
#创建nginx用户和用户组
groupadd nginx 
useradd nginx -g nginx -s /sbin/nologin -M
#安装nginx编译需要的环境 
yum -y install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel
  • 进入软件目录
cd /usr/local/src
  • 下载nginx源码

打开http://nginx.org/en/download.html

wget http://nginx.org/download/nginx-1.18.0.tar.gz

解压缩

tar -zxvf nginx-1.18.0.tar.gz

进入nginx文件夹

ll
cd nginx-1.18.0

编译前检查,将软件

./configure --prefix=/usr/local/nginx

由于事先已经安装了linux的编译环境,因此此处没有报错。

  • 安装nginx
make && make install

显示nginx安装的位置在/usr/local/nginx

  • 启动nginx
cd /usr/local/nginx/sbin
./nginx

此时没有报错,在浏览器输入ip即可看到nginx的显示如下:

若此时显示proxy 错误,需要关闭防火墙,systemctl stop firewalld即可

关闭nginx

/usr/local/nginx/sbin/nginx -s stop #停止

相关命令:
/usr/local/nginx/sbin/nginx # 启动
/usr/local/nginx/sbin/nginx -s reload #重新加载配置

  • 获取nginx的位置和配置文件的位置
whereis nginx

find / | grep nginx.conf

  • 查看进程
ps -ef | grep nginx

  • 修改nginx的配置文件

添加配置文件的文件夹

mkdir /usr/local/nginx/conf/conf.d
nano /usr/local/nginx/conf/nginx.conf

修改文件如下:

  • 添加到系统服务
nano /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

参数说明:

注意:[Service]的启动、重启、停止命令全部要求使用绝对路径

[Unit]:服务的说明
Description:描述服务
After:描述服务类别
[Service]服务运行参数的设置
Type=forking是后台运行的形式
ExecStart为服务的具体运行命令
ExecReload为重启命令
ExecStop为停止命令
PrivateTmp=True表示给服务分配独立的临时空间
[Install]运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3
  • 设置开机启动
systemctl enable nginx
systemctl start nginx
systemctl status nginx

若此时显示如下:

需要手动停止nginx

/usr/local/nginx/sbin/nginx -s stop

再重新设置开机启动

  • 最终

nginx的安装位置:/usr/local/nginx/

配置位置:/usr/local/nginx/conf

系统服务的控制命令/usr/local/nginx/conf/conf.d

systemctl restart nginx

  • 补充说明

在安装成功的nginx中添加未编译的安装模块,nginx添加模块需要重新编译nginx,而不是像Apache一样配置文件引用动态链接库。

参考资料:

  1. https://www.jianshu.com/p/e47be1052dc3
  2. https://blog.csdn.net/annita2019/article/details/104520243
  3. https://www.cnblogs.com/djlsunshine/p/11255709.html
  4. https://www.jianshu.com/p/4134a44a09c2

欢迎回来

希望本站对你有所帮助!

如有疑问请联系info@fanlog.org
2023 年 11 月
一二三四五六日
 12345
6789101112
13141516171819
20212223242526
27282930 
« 6 月    

AI辅助 (17)

© 2025 | Powered by Superbs Personal Blog theme
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept All”, you consent to the use of ALL the cookies. However, you may visit "Cookie Settings" to provide a controlled consent.
Cookie SettingsAccept All
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT