centos7 编译安装nginx

回复 收藏
本帖最后由 gxp2008 于 2016-8-18 16:42 编辑


  最小化安装centos:

下载地址:http://mirrors.yun-idc.com/cento ... -x86_64-Minimal.iso

关闭selinux

sed –i ‘s/SELINUX=enforcing/SELINUX=disabled/g’ /etc/selinux/config

开始安装nginx1.7.8

创建群组groupadd www创建一个用户,不允许登陆和不创主目录 useradd -s /sbin/nologin -g www -M www#下载最新版nginxwget -C http://nginx.org/download/nginx-1.7.8.tar.gztar zxvf nginx-1.7.8.tar.gz#编译安装./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_gzip_static_module
makemake install

如果有错误提示:

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

解决方法:

yum install gcc gcc-c++

如果有错误提示:

./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= option.

解决方法:

yum install pcre-devel

如果有错误提示:

./configure: error: SSL modules require the OpenSSL library.
You can either do not enable the modules, or install the OpenSSL library
into the system, or build the OpenSSL library statically from the source
with nginx by using –with-openssl= option.

解决方法:

yum install openssl-devel

以上错误提示依次解决后:再一次的运行

./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module  --with-http_gzip_static_module
make
meke install
同样的 echo $? 看看是否返回0和报错

编译参数解释:

#指定运行权限的用户    --user=www#指定运行的权限用户组  --group=www#指定安装路径       --prefix=/usr/local/nginx#支持nginx状态查询--with-http_stub_status_module#开启ssl支持--with-http_ssl_module#开启GZIP功能--with-http_gzip_static_module

因此要顺利的通过nginx编译安装必须安装的依赖关系有:

yum install gc gcc gcc-c++ pcre-devel zlib-devel openssl-devel
2、在 centos7 中为nginx的启动、重启、重载配置添加脚本

nginx直接启动的方法:

/usr/local/nginx/sbin/nginx

但是不是很方便,因此使用下面的脚本来控制nginx的启动关闭重载更加合理一些。

编辑文件:vim /usr/lib/systemd/system/nginx.service 添加下面的脚本,注意路径 !

给启动脚本权限: chmod 755 !$

[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


在一些主机上,nginx启动脚本不正常可以这样改写 nginx.service 。如果已经用命令启动nginx需要kill掉master主进程,然后在用systemctl start 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
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target

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 #开机运行
nginxsystemctl disable nginx.service #取消开机运行
nginxsystemctl 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 #显示启动失败的服务

日志切割脚本里面 systemctl reload nginx.service这样写

2016-08-11 17:05 举报
已邀请:

回复帖子,请先登录注册

退出全屏模式 全屏模式 回复
评分
可选评分理由: