小知识:CentOS7安装Nginx并配置自动启动的方法步骤

1、官网下载安装包

http://nginx.org/en/download.html,选择适合Linux的版本,这里选择最新的版本,下载到本地后上传到服务器或者centos下直接wget命令下载。

%小知识:CentOS7安装Nginx并配置自动启动的方法步骤-猿站网-插图

切换到/usr/local目录,下载软件包

?
1
2
# cd /usr/local
# wget http://nginx.org/download/nginx-1.11.5.tar.gz

2、安装nginx

先执行以下命令,安装nginx依赖库,如果缺少依赖库,可能会安装失败,具体可以参考文章后面的错误提示信息。

?
1
2
3
4
5
6
7
# yum install gcc-c++
# yum install pcre
# yum install pcre-devel
# yum install zlib
# yum install zlib-devel
# yum install openssl
# yum install openssl-devel

解压安装包

?
1
# tar -zxvf nginx-1.11.5.tar.gz

nginx被解压到了/usr/local/nginx-1.11.5 目录下(不要把压缩包解压到/usr/local/nginx目录下,或者将解压后的目录重命名为nginx,因为nginx会默认安装到/usr/local/nginx目录下),切换到nginx-1.11.5/目录

?
1
# cd /usr/local/nginx-1.11.5/

执行# ./configure

?
1
# ./configure

该操作会检测当前系统环境,以确保能成功安装nginx,执行该操作后可能会出现以下几种提示:

    checking for OS

     + Linux 3.10.0-123.el7.x86_64 x86_64

    checking for C compiler … not found

    ./configure: error: C compiler cc is not found

    如果出现以上错误提示信息,执行yum install gcc-c++安装gcc,

    ./configure: error: the HTTP rewrite module requires the PCRE library.

    You can either disable the module by using –without-http_rewrite_module

    option, or install the PCRE library into the system, or build the PCRE library

    statically from the source with nginx by using –with-pcre=<path> option.

    如果出现上面提示,表示缺少PCRE库

    ./configure: error: the HTTP gzip module requires the zlib library.

    You can either disable the module by using –without-http_gzip_module

    option, or install the zlib library into the system, or build the zlib library

    statically from the source with nginx by using –with-zlib=<path> option.

    如果出现以上提示,表示缺少zlib库

 如果没有出现./configure: error提示,表示当前环境可以安装nginx,执行make和make install编译nginx

?
1
2
# make
# make install

没有出错的话,表示nginx已经成功安装完成,默认安装位置为/usr/local/nginx,之前的/usr/local/nginx-1.11.5/可以删除掉了。

如果出现cp: conf/koi-win and /usr/local/nginx/conf/koi-win are the same file,可能是你把安装包解压到了/usr/local/nginx目录,解决办法是将该目录重命名为其他名称后再执行make,make install.

3、配置nginx开机启动

切换到/lib/systemd/system/目录,创建nginx.service文件vim nginx.service

?
1
2
# cd /lib/systemd/system/
# vim nginx.service

文件内容如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx reload
ExecStop=/usr/local/nginx/sbin/nginx quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target

退出并保存文件,执行systemctl enable nginx.service使nginx开机启动

?
1
# systemctl enable nginx.service

systemctl start nginx.service    启动nginx

systemctl stop nginx.service    结束nginx

systemctl restart nginx.service    重启nginx

4、验证是否安装成功

输入http://服务器IP/ 如果能看到nginx的界面,就表示安装成功了

%小知识:CentOS7安装Nginx并配置自动启动的方法步骤-1猿站网-插图

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://www.zhaoweihua.cn/article/30/nginx1.11-centos7.html

声明: 猿站网有关资源均来自网络搜集与网友提供,任何涉及商业盈利目的的均不得使用,否则产生的一切后果将由您自己承担! 本平台资源仅供个人学习交流、测试使用 所有内容请在下载后24小时内删除,制止非法恶意传播,不对任何下载或转载者造成的危害负任何法律责任!也请大家支持、购置正版! 。本站一律禁止以任何方式发布或转载任何违法的相关信息访客发现请向站长举报,会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。本网站的资源部分来源于网络,如有侵权烦请发送邮件至:2697268773@qq.com进行处理。
建站知识

小知识:Linux中拷贝 cp命令中拷贝所有的写法详解

2023-4-12 3:02:56

建站知识

小知识:linux C编程常见的错误总结(必看篇)

2023-4-12 3:11:04

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索