nginx安装配置步骤

发布时间:2022-07-04 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了nginx安装配置步骤脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

nginx安装配置步骤

1.安装基本软件

​ yum install gcc gcc-c++ pcre pcre-devel oPEnssl openssl-devel zlib zlib-devel -y

2.创建存放文件的文件夹
[root@nginx1 apps]# cd /opt/
[root@nginx1 apps]# mkdir apps
[root@nginx1 apps]# cd apps/
将nginx压缩包上传到本路径下
解压:
[root@nginx1 apps]# tar -zxvf nginx-1.16.1.tar.gz
[root@nginx1 apps]# cd nginx-1.16.1/
[root@nginx1 nginx-1.16.1]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  htML  LICENSE  man  README  src
[root@nginx1 nginx-1.16.1]#
3.目录说明

auto: 存放nginx自动安装的相关文件

conf:存放nginx服务器配置文件

configure:命令,用于对即将安装的软件的配置,完成makefile编译文件的生成

contrib:存放由其他机构贡献的文档材料

html:存放nginx欢迎界面

man:manual,手册,存放nginx帮助文档

src:存放nginx源码

4.生成makefile

​ 在nginx解压目录下运行make命令,用于完成编译。但此时会给出提示:没有指定目标,并且没有发现编译文件makefile.编译命令make需要根据编译文件makefile进行编译,所以在编译之前需要先生成编译文件makefile.使用configure命令可以生成该文件。那么,configure命令需要配置哪些参数呢?使用--help可以查看到可以使用的参数说明。这些参数可以分为三类:

第一类:基本信息的配置

第二类:默认没有安装,可以指定安装的模块,使用--wITh开头。nginx的高拓展性就体现在这里

第三类:默认已经安装,可以指定卸载的模块,使用--without开头

​ 下面是简单配置的命令执行。命令中每一行的最后添加了反斜杠表示当前命令并未结束,回车不会执行该命令。执行成功护,会给出配置报告。下面以安装对https访问协议支持的模块http_ssl_module为例

​ --PRefix:用于指定nginx的安装目录。注意,安装目录与解压目录不一样。

​ --http_ssl_module:https访问协议需要安装http安全连接协议模块SSL(Secure SocketsLayer,安全套接层)。注意,在执行过configure命令后并不会立即生成/usr/local/nginx目录,也不会马上开始安装指定的模块,而仅仅是将命令中指定的参数及默认配置写道即将要生成的makefile文件中。

配置报告以两部分构成:第一部分给出了配置的系统库;第二部分给出了系统配置信息。

​ path prefix: 指定nginx安装目录

​ binary file: nginx命令文件

​ modules path: nginx模块存放路径

​ configuration prefix: nginx配置文件存放路径

​ onfiguraion file: nginx配置文件名

​ pid file: nginx的进程id文件

​ error LOG file:错误日志文件

​ http access log file: http访问日志文件

​ http xxx: 其他http请求相关的文件。

配置成功后,再次查看nginx解压目录,发现其中多出了一个文件makefile。后面的编译就是依靠该文件进行的。

[root@nginx1 nginx-1.16.1]# mkdir -p /VAR/tmp/nginx/client
[root@nginx1 nginx-1.16.1]# pwd
/opt/apps/nginx-1.16.1
[root@nginx1 nginx-1.16.1]# ./configure 
> prefix=/opt/nginx 
> sbin-path=/usr/sbin/nginx 
> conf-path=/etc/nginx/nginx.conf 
> error-log-path=/var/log/nginx/error.log 
> http-log-path=/var/log/nginx/access.log 
> pit-path=/var/run/nginx/nginx.pid 
> lock-path=/var/lock/nginx.lock 
> user=nginx 
> group=nginx 
> with-http_ssl_module 
> with-http_flv_module 
> with-http_stub_status_module 
> with-http_gzip_static_module 
> http-client-body-temp-path=/var/tmp/nginx/client/ 
> http-prxy-temp-path=/var/tmp/nginx/Proxy/ 
> http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ 
> http-uwsgi-temp-path=/var/tmp/nginx/uwsgi 
> http-scgi-temp-path=/var/tmp/nginx/scgi 
> with-pcre
注意:/var/tmp/nginx/client目录需要手动创建

也可以使用简单安装,指定安装目录和https访问支持

[root@nginx1 nginx-1.16.1]# pwd
/opt/apps/nginx-1.16.1
[root@nginx1 nginx-1.16.1]# ./configure 
> prefix=//nginx 
> with-http_ssl_module 
> with-http_gzip_static_module 
> error-log-path=/var/log/nginx/nginx.log 
> pid-path=/var/log/nginx.pid
4.执行configure文件,生成makefile文件
[root@nginx1 nginx-1.16.1]# pwd
/opt/apps/nginx-1.16.1
[root@nginx1 nginx-1.16.1]# ./configure
checking for OS
 + Linux 3.10.0-1160.el7.x86_64 x86_64
checking for C compiler ... found
 + using GNU C compiler
 + gcc version: 4.8.5 20150623 (red hat 4.8.5-44) (GCC) 
checking for gcc -pipe switch ... found
checking for -Wl,-E switch ... found
....
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
[root@nginx1 nginx-1.16.1]#
5.编译安装

这是两个命令,make为编译命令,make install 为安装命令,可以分别执行。这里使用&&将这两个命令连接执行,会在前面命令执行成功的前提下才会执行第二个命令。

[root@nginx1 nginx-1.16.1]# pwd
/opt/apps/nginx-1.16.1
[root@nginx1 nginx-1.16.1]# make && make install
6.Centos7源码包安装Nginx,添加到服务列表,设置开机自启
https://blog.csdn.net/QQ_36194413/article/details/85841097
7.nginx命令随处可用

在nginx的安装目录/opt/nginx中有一个sbin目录,其中存放着nginx的命令程序nginx。默认情况下,若要使用nginx命令,则必须要在/opt/nginx/sbin目录中,或指定命令路径,使用起来很不方便。为了能够在任意目录下均可直接执行nginx命令。

  1. 将nginx添加为自定义系统服务:
[root@nginx ~]# vi /usr/lib/systemd/System/nginx.service

输入如下内容:

[Unit]
Description=nginx - High performance web server, proxy server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/var/log/nginx.pid
ExecStartPre=/opt/nginx/sbin/nginx -t -c /opt/nginx/conf/nginx.conf
ExecStart=/opt/nginx/sbin/nginx -c /opt/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target
  1. 设置nginx服务开机自启动并重新启动nginx服务
[root@nginx /]# systemctl enable nginx.service
[root@nginx /]# systemctl restart nginx.service

基本操作示例:

1. 启动服务

​ systemctl start nginx.service

2. 重启服务

​ systemctl restart nginx.service

3. 重载服务

​ systemctl reload nginx.service

4. 停止服务

​ systemctl stop nginx.service

  1. 将nginx命令加入到path和建立软连接,这样就可以直接使用 nginx命令
[root@nginx /]# ln -s  /opt/nginx/sbin/nginx /usr/local/sbin/nginx
[root@nginx /]# vi ~/.bash_profile
在export PATH 后面添加nginx命令的路径,例如PATH=$HADOOP_PREFIX/bin:$PATH:$HOME/bin:$SPARK_HOME/bin:/opt/nginx/sbin/nginx
  1. 常用nginx命令
    • 检查配置文件正确性: nginx -t
    • 重新加载配置文件: nginx -s reload
    • 查看nginx版本和模块信息: nginx -V
nginx启动失败错误日志:
[root@nginx1 ~]#systemctl service nginx start
Starting nginx (via systemctl):  Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.

[root@nginx1 ~]# systemctl status nginx.service 
● nginx.service - SYSV: Nginx is an HTTP(S) server,HTTP(S) reverse
   Loaded: loaded (/etc/rc.d/init.d/nginx; bad; vendor preset: disabled)
   Active: failed (Result: exit-code) since 二 2021-09-28 08:02:35 CST; 42s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 1369 ExecStart=/etc/rc.d/init.d/nginx start (code=exited, status=1/FAILURE)

9月 28 08:02:35 nginx1 systemd[1]: Starting SYSV: Nginx is an HTTP(S) server,HTTP(S) reverse...
9月 28 08:02:35 nginx1 nginx[1369]: /etc/rc.d/init.d/nginx:行20: ${basename $nginx}: 坏的替换
9月 28 08:02:35 nginx1 systemd[1]: nginx.service: control process exited, code=exited status=1
9月 28 08:02:35 nginx1 systemd[1]: Failed to start SYSV: Nginx is an HTTP(S) server,HTTP(S) reverse.
9月 28 08:02:35 nginx1 systemd[1]: Unit nginx.service entered failed state.
9月 28 08:02:35 nginx1 systemd[1]: nginx.service failed.

脚本宝典总结

以上是脚本宝典为你收集整理的nginx安装配置步骤全部内容,希望文章能够帮你解决nginx安装配置步骤所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。