Linux Centos7 安装Nginx

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

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

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-pathhttp日志
–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文件

7.执行make编译

make

8.安装,执行

make install

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

cd /usr/local/nginx/sbin

10.执行./nginx启动nginx

./nginx

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

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)

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

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

将pid节点打开,修改为:

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

最后修改如下所示:

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

mkdir /usr/local/nginx/logs

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

/usr/local/nginx/sbin/nginx 

17.总算访问到了

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值