linux环境nginx搭建操作实战

发布时间:2024年01月02日

安装Nginx

安装Nginx

安装Nginx前先安装如下依赖:
安装nginx需要先将官网下载的源码进行编译,编译依赖gcc环境,如果没有gcc环境,需要安装gcc.

PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式,所以需要在linux上安装pcre库。

zlib库提供了很多种压缩和解压缩的方式,nginx使用zlib对http包的内容进行gzip,所以需要在linux上安装zlib库。

OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。
nginx不仅支持http协议,还支持https(即在ssl协议上传输http),所以需要在linux安装openssl库。

1.gcc 安装:yum install gcc-c++

2.PCRE pcre-devel 安装:yum install -y pcre pcre-devel

3.zlib 安装: yum install -y zlib zlib-devel

4.OpenSSL 安装:yum install -y openssl openssl-devel
1.编译安装Nginx
[root@iZwz96pb4qob9lpzadqipkZ local]# cd /usr/local
[root@iZwz96pb4qob9lpzadqipkZ local]# wget https://nginx.org/download/nginx-1.13.4.tar.gz

1.下载
wget https://nginx.org/download/nginx-1.13.4.tar.gz

2.解压
tar -zxvf nginx-1.13.4.tar.gz

3.cd nginx-1.13.4
编译
./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi

可以看到
Configuration summary
  + using system PCRE library
  + OpenSSL library is not used
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/var/run/nginx/nginx.pid"
  nginx error log file: "/var/log/nginx/error.log"
  nginx http access log file: "/var/log/nginx/access.log"
  nginx http client request body temporary files: "/var/temp/nginx/client"
  nginx http proxy temporary files: "/var/temp/nginx/proxy"
  nginx http fastcgi temporary files: "/var/temp/nginx/fastcgi"
  nginx http uwsgi temporary files: "/var/temp/nginx/uwsgi"
  nginx http scgi temporary files: "/var/temp/nginx/scgi"

编译安装
4.make
5.make install
2.安装成功查看安装目录
test -d '/usr/local/nginx/html' \
        || cp -R html '/usr/local/nginx'
test -d '/var/log/nginx' \
        || mkdir -p '/var/log/nginx'
make[1]: Leaving directory `/usr/local/nginx-1.13.4'
[root@iZwz96pb4qob9lpzadqipkZ nginx-1.13.4]# cd ..
[root@iZwz96pb4qob9lpzadqipkZ local]# ls
aegis  games    lib64    nginx-1.13.4         share
bin    include  libexec  nginx-1.13.4.tar.gz  src
etc    lib      nginx    sbin
[root@iZwz96pb4qob9lpzadqipkZ local]# cd nginx
[root@iZwz96pb4qob9lpzadqipkZ nginx]# ll
total 12
drwxr-xr-x 2 root root 4096 Aug 25 00:08 conf
drwxr-xr-x 2 root root 4096 Aug 25 00:08 html
drwxr-xr-x 2 root root 4096 Aug 25 00:08 sbin
[root@iZwz96pb4qob9lpzadqipkZ nginx]# 
3.运行Nginx,进入到sbin目录下执行./nginx,出现这个错误只需root下手动创建目录即可。
[root@iZwz96pb4qob9lpzadqipkZ sbin]# ./nginx
nginx: [emerg] mkdir() "/var/temp/nginx/client" failed (2: No such file or directory)
[root@iZwz96pb4qob9lpzadqipkZ sbin]# pwd
/usr/local/nginx/sbin
[root@iZwz96pb4qob9lpzadqipkZ sbin]# ./nginx
nginx: [emerg] mkdir() "/var/temp/nginx/client" failed (2: No such file or directory)
[root@iZwz96pb4qob9lpzadqipkZ sbin]# mkdir -p /var/temp/nginx/client
[root@iZwz96pb4qob9lpzadqipkZ sbin]# ./nginx

查看配置信息是否正确

[root@iZwz96pb4qob9lpzadqipkZ local]/usr/local/nginx/sbin/nginx -t

在浏览器使用ip访问
出现此界面则代表nginx成功运行
出现此界面则代表nginx成功运行

参考学习: https://blog.csdn.net/ww646904527/article/details/77544882?spm=1001.2014.3001.5501

文章来源:https://blog.csdn.net/good6666zl/article/details/135289581
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。