nginx安装配置步骤

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

[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]#

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

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

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

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

html:存放nginx欢迎界面

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

src:存放nginx源码

​ 在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
[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]#

这是两个命令,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
https://blog.csdn.net/qq_36194413/article/details/85841097

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

[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
[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.

Original: https://www.cnblogs.com/Boyka1/p/15366196.html
Author: 程胥员
Title: nginx安装配置步骤

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/598520/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球