centos7安装nginx

2016-06-15

centos7启动服务的方式为systemctl start nginx.

1、yum安装方法

rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

yum -y install nginx(yum -y remove nginx)

rpm -qf nginx

rpm -ql nginx
2、编译安装

#!/bin/sh

yum -y update

export DEFAULT=/usr/local/nginx/conf/default
export LNMP=/opt/LNMP
export CONF=/usr/local/nginx/conf
LOG=/data/logs/nginx
cd $LNMP

#wget -c http://nginx.org/download/nginx-1.10.1.tar.gz -P ${LNMP}/untar
#wget -c ftp://61.135.158.199/pub/openssl-1.0.1i.tar.gz -P ${LNMP}/untar

tar zxf untar/pcre-8.13.tar.gz
pushd pcre-8.13
./configure;make;make install
popd
tar zxf untar/nginx-1.10.1.tar.gz
tar jxf untar/zlib-1.2.5.tar.bz2
tar zxf untar/openssl-1.0.1i.tar.gz

pushd nginx-1.10.1
./configure \
--prefix=/usr/local/nginx \
--sbin-path=/usr/sbin \
--with-http_ssl_module \
--with-http_sub_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-zlib=../zlib-1.2.5 \
--with-openssl=../openssl-1.0.1i \
--with-pcre=../pcre-8.13
make;make install
popd
groupadd www
useradd -g www -d /dev/null -s /sbin/nologin www

/bin/cp $LNMP/conf/nginx.service /usr/lib/systemd/system
mkdir ${CONF}/default
mv ${CONF}/*.default ${CONF}/default
/bin/cp $LNMP/conf/*.conf ${CONF}
/bin/cp $LNMP/conf/star.* ${CONF}
/bin/cp $LNMP/conf/root /var/spool/cron/
mkdir -p ${LOG}

systemctl start nginx
systemctl enable nginx
netstat -ntlp |grep 80
3、配置服务
编辑文件:vim /usr/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=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT 搜索$MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

4、systemctl的一些使用方法:
systemctl is-enabled servicename.service #查询服务是否开机启动
systemctl enable xxx.service #开机运行服务
systemctl disable xxx.service #取消开机运行
systemctl start xxx.service #启动服务
systemctl stop xxx.service #停止服务
systemctl restart xxx.service #重启服务
systemctl reload xxx.service #重新加载服务配置文件
systemctl status xxx.service #查询服务运行状态
systemctl --failed #显示启动失败的服务

因此,添加上面脚本后,centos7 中操作nginx的方法有
systemctl is-enabled nginx.service #查询nginx是否开机启动
systemctl enable nginx.service #开机运行nginx
systemctl disable nginx.service #取消开机运行nginx
systemctl start nginx.service #启动nginx
systemctl stop nginx.service #停止nginx
systemctl restart nginx.service #重启nginx
systemctl reload nginx.service #重新加载nginx配置文件
systemctl status nginx.service #查询nginx运行状态
systemctl --failed #显示启动失败的服务

 

分类:操作系统 | 标签: |

相关日志

评论被关闭!