欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Linux Centos7 安装Nginx

程序员文章站 2022-07-06 17:31:12
...

1、去官网http://nginx.org/en/download.html下载稳定版本的Nginx

Linux Centos7 安装Nginx

2、上传到自己服务,这里我上传到/home/software

Linux Centos7 安装Nginx

3、安装依赖环境

  安装前提:能ping通外网(ping不通的,需要按照自己实际情况解决后才能执行以下步骤)

ping baidu.com

  3.1安装gcc环境

       执行:  

yum install gcc-c++

可能会出现以下情况:

已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=os&infra=stock error was
14: curl#6 - "Could not resolve host: mirrorlist.centos.org; 未知锟侥达拷锟斤拷"


 One of the configured repositories failed (未知),
 and yum doesn't have enough cached data to continue. At this point the only
 safe thing yum can do is fail. There are a few ways to work "fix" this:

     1. Contact the upstream for the repository and get them to fix the problem.

     2. Reconfigure the baseurl/etc. for the repository, to point to a working
        upstream. This is most often useful if you are using a newer
        distribution release than is supported by the repository (and the
        packages for the previous distribution release still work).

     3. Run the command with the repository temporarily disabled
            yum --disablerepo=<repoid> ...

     4. Disable the repository permanently, so yum won't use it by default. Yum
        will then just ignore the repository until you permanently enable it
        again or use --enablerepo for temporary usage:

            yum-config-manager --disable <repoid>
        or
            subscription-manager repos --disable=<repoid>

     5. Configure the failing repository to be skipped, if it is unavailable.
        Note that yum will try to contact the repo. when it runs most commands,
        so will have to try and fail each time (and thus. yum will be be much
        slower). If it is a very temporary problem though, this is often a nice
        compromise:

            yum-config-manager --save --setopt=<repoid>.skip_if_unavailable=true

解决办法:执行如下命令:

vi /etc/resolv.conf

然后添加如下内容:

nameserver 8.8.8.8 
nameserver 8.8.4.4 
serchdomain localdomain

3.2安装PCRE库,用于解析正则表达式

yum install -y pcre pcre-devel

3.3安装zlib压缩和解压缩依赖

yum install -y zlib zlib-devel

3.4安装openssl,SSL安全加密的套接字协议层,用于HTTP安全传输,也就是https。

yum install -y openssl openssl-devel

4.进入到上传的nginx目录,解压上传上来的Nginx压缩包(压缩包名称根据自己的)

tar -zxvf nginx-1.18.0.tar.gz

5、编译前,先创建nginx临时目录,如果不创建,在启动nginx过程中会报错。

mkdir /var/temp/nginx -p

6.在解压缩的nginx目录下(这里我的是在/home/software/nginx-1.18.0目录下),执行如下命令

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

这些前缀的含义可以参考如下:

–prefix 指定nginx安装目录
–pid-path 指向nginx的pid
–lock-path 锁定安装文件,防止被恶意篡改或误操作
–error-log 错误日志
–http-log-path http日志
–with-http_gzip_static_module 启用gzip模块,在线实时压缩输出数据流
–http-client-body-temp-path 设定客户端请求的临时目录
–http-proxy-temp-path 设定http代理临时目录
–http-fastcgi-temp-path 设定fastcgi临时目录
–http-uwsgi-temp-path 设定uwsgi临时目录
–http-scgi-temp-path 设定scgi临时目录

执行完成后,获取到Makefile文件

Linux Centos7 安装Nginx

7.执行make编译

make

8.安装,执行

make install

9.安装成功后,进入nginx的sbin目录下(安装命令--prefix=/usr/local/nginx 指定的目录)

cd /usr/local/nginx/sbin

10.执行./nginx启动nginx

./nginx

11、访问下,因为我的是虚拟机,这个IP是我自己设置的,访问自己的

Linux Centos7 安装Nginx

12.emmm~~~好吧。80端口没开放,开放80端口(也可以直接关闭防火墙)。执行下如下命令二选一,并重启虚拟机。。

#以下命令二选一
#开放80端口
firewall-cmd --zone=public --add-port=80/tcp --permanent

#关闭防火墙
systemctl stop firewalld

13.重启后,发现又报错了:

nginx: [emerg] open() "/var/run/nginx/nginx.pid" failed (2: No such file or directory)

Linux Centos7 安装Nginx

14.执行vi命令,修改配置:

vi /usr/local/nginx/conf/nginx.conf

将pid节点打开,修改为:

pid         /usr/local/nginx/logs/nginx.pid;

最后修改如下所示:

Linux Centos7 安装Nginx

15.创建logs目录,执行如下命令:

mkdir /usr/local/nginx/logs

16.执行/usr/local/nginx/sbin/nginx (按照自己的nginx启动脚本位置来)启动nginx

/usr/local/nginx/sbin/nginx 

17.总算访问到了

Linux Centos7 安装Nginx

,重启服务器也不会报错了。

Linux Centos7 安装Nginx