1、编译安装
1.1、安装编译工具
首先需要安装各种必须工具:
GCC
GCC-C++
Autoconf
automake
[root@localhost /]# yum install gcc gcc-c++ autoconfautomake
还需要安装一些专用的库:
支持gzip功能的:zlib库
rewrite模块:pcre库
ssl功能:openssl库
[root@localhost /]# yum install zlib zlib-devel pcrepcre-devel openssl openssl-devel
1.2、开始安装
安装时,首先需要从Nginx网站下载Nginx安装包,比如本例:nginx-1.10.1.tar.gz
该安装包是源码包,需要本地编译才可以安装运行。
将安装源码包解压到某个目录下:
[root@myhost server]# tar–zxvf nginx-1.10.1.tar.gz
然后开始编译Nginx源码,进入nginx目录,然后执行三步编译:
[root@localhost nginx-1.10.1]#./configure --prefix=/usr/local/nginx && make && make install
注:安装必须放在一个不存在的目录下,/usr/local/nginx会自动生成
编译安装之后,Nginx将会被默认安装到路径“/usr/local/nginx”下。
cd ../nginx返回上级目录再进入nginx目录
1.3执行:./nginx -t
[root@localhost sbin]#./nginx -t
查看是否有进程占用80端口,若有则kill -9 进程号杀进程,如httpd .
[root@localhost nginx]#ps -ef |grep httpd
[root@localhost nginx]#kill -9 进程号
1.4检查是否安装成功
首先进入/usr/local/nginx/sbin目录下
再执行./nginx -t 检查nginx安装成功
[root@localhost sbin]# ./nginx –t
打开网页验证:
2.配置开机自启动
需要写一个服务脚本,将Nginx作为CentOS的服务,随开机自行启动。
在“/lib/systemd/system”中创建文件nginx.service:
[root@localhost /]# vi /lib/systemd/system/nginx.service
然后将如下内容根据具体情况进行修改后,添加到nginx.service文件中:
[Unit]
Description=nginx1.10.1
After=network.target remote-fs.targetnss-lookup.target
[Service]
Type=forking
PIDFile=/var/run/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
:wq保存
使配置生效:
[root@localhost /]# systemctl enable nginx.service
然后重启linux系统,或者彻底杀死Nginx
[root@localhost /]# pkill -9 nginx
最终执行自动启动任务
[root@localhost /]# systemctl start nginx.service