centos7 下 Nginx安装

695 阅读2分钟

一、前置环境

nginx是c语言写的,所以需要安装gcc环境,以供其编译。

1、gcc 安装

[root@localhost softhere]# yum install gcc-c++

碰到的一个问题:

[root@localhost softhere]# yum -y install gcc
已加载插件: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

Cannot find a valid baseurl for repo: base/7/x86_64

解决方案:配置ens33 添加dns blog.csdn.net/qq_44543508…

2、PCRE pcre-devel 安装

PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也需要此库。命令:

[root@localhost softhere]# yum install -y pcre pcre-devel

3、zlib 安装

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

yum install -y zlib zlib-devel

4、OpenSSL 安装

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

yum install -y openssl openssl-devel

5、下载nginx

目前最新的稳定版

nginx.org/en/download…

6、解压

[root@localhost softhere]# tar -zxvf nginx-1.18.0.tar.gz -C /opt/module/

二、安装nginx

1、进入nginx目录,执行命令configure

[root@localhost nginx-1.18.0]# ./configure

或者安装到指定位置:

./configure \
--prefix=/home/nginx/ \
--sbin-path=/home/nginx/nginx \
--conf-path=/home/nginx/conf/nginx.conf \
--pid-path=/home/nginx/nginx.pid \

2、执行make命令

make

3、执行make install命令

make install

4、配置nginx.conf (在/usr/local/nginx/conf下)

尽量不要占用80端口

 server {
        listen       80;
        server_name  192.168.142.100;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

5、启动nginx

先进入 nginx安装的目录,注意这是在/usr/local下面

cd /usr/local/nginx/sbin/

启动 : ./nginx

停止: ./nginx -s stop

重启: ./nginx -s reload

启动后,能看到这个页面就可以代表安装成功了。