一,通过yum命令安装

1、操作系统版本的确认很重要,因为下一步我们要依靠这个来创建yum源文件

2、创建 nginx.repo

vi  /etc/yum.repos.d/nginx.repo 

文件内容如下(如果是centos7,则对应的把数字6改成7):

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/6/$basearch/
gpgcheck=0
enabled=1

3、清除缓存并建立新的缓存

yum clean all

yum makecache

4、使用yum install nginx命令进行安装,输入y并回车

5、使用service nginx start命令启动nginx

service nginx start

6、查看Nginx进程是否存在

二、通过下载源码包进行安装

 https://blog.csdn.net/darkdragonking/article/details/79034654

 https://www.cnblogs.com/yaoximing/p/6068622.html

一、安装依赖

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

yum install gcc-c++

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

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

二、下载源码(去官网下载最新稳定版)

wget -c https://nginx.org/download/nginx-1.14.2.tar.gz

三、解压、编译

tar -zxvf nginx-1.14.2.tar.gz 
cd nginx-1.14.2/
./configure
make && make install

四、启动以及停止

cd /usr/local/nginx/sbin/
./nginx 
./nginx -s stop
./nginx -s quit
./nginx -s reload

五、设置开机自启

即在rc.local增加启动代码就可以了

vim /etc/rc.local

增加一行

/usr/local/nginx/sbin/nginx 

设置执行权限:

chmod 755 rc.local

六、用systemctl管理

创建配置文件 

源码安装的nginx在/etc/systemd/system/multi-user.target.wants/目录下是没有nginx.service这个文件的,所以要新建

[root@localhost nginx-1.14.0]#vim /usr/lib/systemd/system/nginx.service

写入内容(全部复制进去,然后修改)

[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID

[Install]
WantedBy=multi-user.target

修改 [Service]内容

 将:
     ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf,
 改为:
     ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

设置开机启动

[root@localhost nginx-1.14.0]# systemctl enable nginx.service

关闭之前启动的nginx服务

[root@localhost nginx-1.14.0]# pkill -9 nginx

重载修改过的所有配置文件

[root@localhost nginx-1.14.0]#systemctl daemon-reload

重新启动nginx服务

[root@localhost nginx-1.14.0]#systemctl start nginx
posted on 2018-11-23 14:30  23云恋49枫  阅读(3670)  评论(1编辑  收藏  举报