一、yum安装
1.默认情况Centos7中无Nginx的源,最近发现Nginx官网提供了Centos的源地址。因此可以如下执行命令添加源
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
#查看源是否添加成功
yum search nginx
2、安装Nginx
yum install -y nginx
3.启动Nginx并设置开机自动运行
systemctl start nginx.service
systemctl enable nginx.service
以下是Nginx的默认路径:
二、源码安装
#创建用户、组
groupadd nginx
useradd nginx -M -s /sbin/nologin -g nginx
依赖包需要先安装
yum -y install zlib zlib-devel openssl openssl--devel pcre pcre-devel
编译参数设置
./configure
--user=nginx \
--group=nginx \
--prefix=/usr/local/nginx \
--sbin-path=/usr/local/nginx/sbin/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--error-log-path=/usr/local/nginx/logs/error.log \
--http-log-path=/usr/local/nginx/logs/access.log \
--pid-path=/usr/local/nginx/logs/nginx.pid \
--http-client-body-temp-path=/usr/local/nginx/tmp/client \
--http-proxy-temp-path=/usr/local/nginx/tmp/proxy \
--http-fastcgi-temp-path=/usr/local/nginx/tmp/fcgi \
--http-uwsgi-temp-path=/usr/local/nginx/tmp/uwsgi \
--http-scgi-temp-path=/usr/local/nginx/tmp/scgi \
--with-pcre \
--with-http_v2_module \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_stub_status_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-http_auth_request_module \
--with-mail \
--with-mail_ssl_module \
--with-file-aio \
--with-threads \
--with-stream \
--with-stream_ssl_module \
编译和安装
#编译和安装
make
make install
检查一下配置
#测试一下配置文件
/usr/local/nginx/sbin/nginx -t
设置拥有者和操作权限
#设置所有者
chown -R nginx:nginx nginx
#设置操作权限
chmod -R 755 nginx
设置开机启动
vim /usr/lib/systemd/system/nginx.service
然后进行编辑
[Unit]
Description=nginx
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
User=nginx
Group=nginx
PrivateTmp=true
[Install]
WantedBy=multi-user.target
编辑保存完后
# 重新加载配置文件
systemctl daemon-reload
##【验证下服务能否配置成功】
# 启动服务
systemctl start nginx
# 关闭服务
systemctl stop nginx
# 重启服务
systemctl restart nginx
# 显示服务的状态
systemctl status nginx
#设置允许开机自启动
systemctl enable nginx
#开放端口
firewall-cmd --zone=public --add-port=80/tcp --permanent
#重新加载
firewall-cmd --reload
三、解决非root用户不能启动nginx的问题
配置好了后,我们通过以下的命令就可以启动nginx了
#启动
/usr/local/nginx/sbin/nginx.sh
这样倒是能启动成功,启动后使用ps -ef|grep nginx
查看一下,master进程是由root启动的,而worker进程是由我们指定的nginx用户执行的。
但实际上我们通常用使用systemctl start nginx
来启动,这时就会报错了,原因就是我们使用非root用户启动。nginx默认使用80端口,但linux规定了root用户可以使用1024以内的端口
,而普通用户只能用1024以上的端口
。如果使用普通用户来启动nginx,会报如下的错
nginx:[warn] the "user" directive makes sense only if the master proce***uns with super-user privileges
解决方法有以下几种(参考博客:Linux非root用户程序使用小于1024端口)
1.SetUID
chmod u+s /usr/local/nginx/sbin/nginx
其实就是赋予这个nignx二进制文件,能够像在root下运行
优点是,方便简单,缺点是既然sudo权限都不给了。这个set uid 最后也是让nginx运行在root权限下。 ps -ef |grep nginx 查看的时候,nginx的主进程是运行在root下的。 虽然是可以让普通用户运行nginx服务,但是不是所有nginx进程都在用户本身下运行
2.端口转发
把监听端口修改成1024以上的端口,然后进行端口转发,把80端口的包转发到1024以上 我们自定义的端口
server {
# listen 80;
# 改为监听8088端口
listen 8088;
# 将80端口的流量转发至8088端口
firewall-cmd --add-forward-port=port=80:proto=tcp:toport=8088 --permanent
# 开放8088端口
firewall-cmd --zone=public --add-port=8088/tcp --permanent
##其它命令
#取消80到8088端口的转发
firewall-cmd --zone=public --remove-forward-port=port=80:proto=tcp:toport=8088 --permanent
#添加80到8088端口的转发(带ip)
firewall-cmd --zone=public --add-forward-port=port=80:proto=tcp:toaddr=xxx.xx.xx.xxx:toport=8088 --permanent
#取消80到8088端口的转发(带ip)
firewall-cmd --zone=public --remove-forward-port=port=80:proto=tcp:toaddr=xxx.xx.xx.xxx:toport=8088 --permanent
优点:可以用第三方用户直接启动,nginx的主进程就是用户本身来启动的。缺点,额外增加开销,负载低的情况可以,负载高了 就不太好了
3.nginx内核超过2.1版本以后出现了能力的说法。
我们可以给/usr/local/nginx/sbin/nginx 赋予监听80端口的权限能力
setcap cap_net_bind_service =eip /usr/local/nginx/sbin/nginx
这样就可以直接用普通用户启用nginx了。在高负载下可减少端口转发产生的负载开销。