nginx的安装

一、安装nginx(源码安装 yum安装)

1.yum安装(两种方法)

方法一:

RPM包获取:http://nginx.org/packages/ :直接下载包——》yum install nginx

方法二:

yum安装法法简单,但是缺少灵活性;无法自定义模块及安装路径
yum源配置:http://nginx.org/en/linux_packages.html

[root@client ~]#yum install yum-utils

进到/etc/yum.repos.d/

[root@client yum.repos.d]# vim nginx.repo

[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[root@client client ~]# yum install nginx
[root@client ~]# nginx -V   #查看nginx模块

2.源码安装

源码安装的环境说明:
系统:CentOS 7.6
软件:nginx-1.15.0.tar.gz
其他所需软件:openssl-1.0.2d.tar.gz、pcre-8.37.tar.gz、zlib-1.2.8.tar.gz
安装方式:源码编译安装
安装位置:/opt/data/nginx
下载地址:http://nginx.org/download
2.1第一步:
安装环境准备,安装依赖包,创建安装目录

[root@localhost ~]# mkdir -p /opt/data/nginx/
[root@localhost ~]#groupadd nginx
[root@localhost ~]# useradd -g nginx nginx
[root@localhost ~]# yum install gcc gcc-c++ make recp pcre-developenssl openssl-devel -y
[root@localhost ~]# tar -xf nginx-1.14.0.tar.gz -C /usr/local/src/

2.2第二步:编译安装

[root@localhost ~]# cd /usr/local/src/nginx-1.15.0/

预编译

[root@localhost nginx-1.15.0]# ./configure --prefix=/opt/data/nginx/ 
--with-http_stub_status_module  --with-http_ssl_module  --with-stream

编译

[root@localhost nginx-1.15.0]# make &make install

2.3第三步:配置环境变量

[root@localhost ~]# cd /opt/data/nginx/sbin/ (注意sbin的路径)
[root@localhost sbin]# ls nginx
[root@localhost sbin]# ./nginx -t
[root@localhost ~]# vim nginx.sh export
PATH=/opt/data/nginx/sbin:$PATH
[root@localhost ~]# source /etc/profied.d/nginx.sh

2.4第四步:Nginx配置
配置文件:规范在etc的目录下。日志文件/var/log

[root@localhost ~]# ln -s /opt/data/nginx/conf /etc/nginx
[root@localhost ~]# cd /etc/nginx/
[root@localhost nginx]# cp -p nginx.conf nginx.conf.bak [root@localhost nginx]# echo “” > nginx.conf
[root@localhost nginx]# vim nginx.conf

user nginx;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
access_log off;
client_max_body_size 128M;
include /etc/nginx/conf.d/*.conf;
}
[root@localhost nginx]# mkdir conf.d
[root@localhost nginx]# mkdir /var/log/nginx
[root@localhost nginx]# chown -R nginx:nginx /var/log/nginx/
第五步:nginx启动
/lib/systemd/system/下是服务的启动文件

[root@localhost ~]#Vim /lib/systemd/system/nginx.service 

[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/opt/data/nginx/sbin/nginx -t -c /opt/data/nginx/conf/nginx.conf
ExecStart=/opt/data/nginx/sbin/nginx -c /opt/data/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
wantedBy=multi-user.target
[root@localhost ~]# systemctl start nginx
[root@localhost ~]# systemctl enable nginx

nginx的安装_第1张图片
Nginx配置文件详解:vim /etc/nginx/nginx.conf
#worker_processes 2; #允许生成的进程数,默认为1 (由cpu核心数决定)
#pid /nginx/pid/nginx.pid; #指定nginx进程运行文件存放地址(pid文件作用:服务重启多次,进程查看还是一次,进程开启后,不再开)
events {
accept_mutex on; #设置网路连接序列化,防止惊群现象发生,默认为on (惊群现象)
multi_accept on; #设置一个进程是否同时接受多个网络连接,默认为off
#use epoll; #事件驱动模型,
nginx的安装_第2张图片
#工作进程的执行者,nobody

你可能感兴趣的:(nginx,运维,centos)