nginx是由俄罗斯工程师Igor Sysoev 用C语言开发的一个免费开源的Web服务器软件,于2004年发布,汇集轻量级、高并发、高性能、低消耗等一系列优势。目前Nginx是互联网上仅次于Apache的第二流行的Web服务器软件。html
接下来咱们开始安装nginx,咱们下面是以centos7为基础环境进行搭建,咱们的安装方法:1、yum安装 2、源码安装。nginx
1 yum install nginx
若是你没有yum 源的话你须要配置下yum源:c++
1 vim /etc/yum.repos.d/nginx.repo
nginx.repo里填入如下内容:vim
1 [nginx] 2 name=nginx repo 3 baseurl=http://nginx.org/packages/centos/7/x86_64/ 4 gpgcheck=0 5 enabled=1
而后咱们清除yum的缓存,进行安装centos
yum clean all yum makecahe yum install nginx
这样咱们就成功安装好了nginx接下来说讲源码安装,源码安装是能够个性化定制的。缓存
首先咱们须要去下载源码包:bash
源码包下载地址:http://nginx.org/en/download.html,咱们选择稳定版的包进行下载服务器
而后咱们进行解压和安装:并发
wget http://nginx.org/download/nginx-1.14.0.tar.gz tar -xzvf nginx-1.14.0.tar.gz cd nginx-1.14.0
建立nginx用户和用户组tcp
useradd -M -s /sbin/nologin nginx
在进行安装以前咱们须要的一些通用配置选项:(官网源码编译配置文档路径:http://nginx.org/en/docs/configure.html )
--prefix=<path> nginx 的安装的根路径,全部其余的安装路径都要依赖于该选项 --sbin-path=<path> 指定nginx二进制文件的路径。若是没有指定,那么这个路径会依赖于--prefix选项 --conf-path=<path> 若是在命令行没有指定配置文件,那么将会经过这里指定的路径,nginx将会去那里查找它的配置文件,这里的路径要精确到文件. --error-log-path=<path> 指定错误日志文件的路径,要精确到文件。 --pid-path=<path>指定pid文件的路径,一般在/var/run/下 --lock-path=<path>互斥锁文件的路径 --user=<user> worker进程运行的用户 --group=<group> worker进程运行的组
--with-http_ssl_module 启用ssl模块
以上是自定义安装的一些配置选项:
源码的安装通常由3个步骤组成:配置(configure)、编译(make)、安装(make install)。接下来咱们开始进行编译安装,我会把安装过程当中的遇到的问题贴出来。
mkdir /opt/nginx &&chown nginx:nginx /opt/nginx/
./configure --prefix=/opt/nginx/ --sbin-path=/usr/bin/ --conf-path=/etc/nginx/nginx.conf --user=nginx --group=nginx
#--prefix=/opt/nginx/ 指定nginx的安装目录是/opt/nginx
#--sbin-path=/usr/bin/ 指定nginx二进制文件的路径是/usr/bin
#--conf-path=/etc/nginx/nginx.conf 指定配置文件的路径
#--user=nginx 指定进程运行的用户
#--group=nginx 指定进程运行的用户
在编译的过程当中我遇到了如下的错误,这里作个记录:
第一个错误 :./configure: error: C compiler cc is not found缺乏gcc编译器。
解决方法:安装gcc编译器
yum -y install gcc-c++ autoconf automake
第二个错误:/configure: error: the HTTP rewrite module requires the PCRE library.确少PCRE库.
解决方法:安装PCRE
yum -y install pcre pcre-devel
第三个错误:./configure: error: the HTTP cache module requires md5 functions from OpenSSL library. You can either disable the module by using --without-http-cache option, or install the OpenSSL library into the system, or build the OpenSSL library statically from the source with nginx by using --with-http_ssl_module --with-openssl=<path> options. 缺乏ssl错误。
解决方法:安装openssl
yum -y install openssl openssl-devel
第四个错误:./configure: error: the HTTP gzip module requires the zlib library. 缺乏zlib库
解决办法:安装zlib库
yum install -y zlib-devel
还有些错误是我没有遇到的,可是我在这边作下记录,之后可能会有帮助。
错误信息:./configure: error: the HTTP XSLT module requires the libxml2/libxslt 缺乏libxml2
解决办法:yum -y install libxml2 libxml2-dev && yum -y install libxslt-devel
错误信息:./configure: error: the HTTP image filter module requires the GD library. You can either do not enable the module or install the libraries.
解决方法:http_image_filter_module是nginx提供的集成图片处理模块,须要gd-devel的支持 yum -y install gd-devel
错误信息:./configure: error: perl module ExtUtils::Embed is required 缺乏ExtUtils
解决方法:yum -y install perl-devel perl-ExtUtils-Embed
错误信息:./configure: error: the GeoIP module requires the GeoIP library. You can either do not enable the module or install the library. 缺乏GeoIP
解决方法:yum -y install GeoIP GeoIP-devel GeoIP-data
以上是配置过程当中可能遇到的错误,接下来咱们进行编译。
make -j 4
-j 参数的意义是并行编译,在多核的状况下能够提高编译速度
安装:
make install
而后咱们须要更改一下目录的全部者和所属组,避免nginx运行时的权限问题
chown -R nginx:nginx /opt/nginx/ chown -R nginx:nginx /etc/nginx/
查看nginx的版本:
[root@test1 etc]# /usr/bin/nginx -v nginx version: nginx/1.14.0
运行nginx
[root@test1 etc]# /usr/bin/nginx [root@test1 etc]# ps -ef |grep nginx root 8984 1 0 10:56 ? 00:00:00 nginx: master process /usr/bin/nginx nginx 8985 8984 0 10:56 ? 00:00:00 nginx: worker process root 8987 3060 0 10:56 pts/2 00:00:00 grep --color=auto nginx
而后咱们就能够直接访问咱们的ip的80端口(端口须要开放的)
若是开了防火墙能够关闭也能够开放80端口。
关闭防火墙:
systemctl stop firewalld
开放80端口:
firewall-cmd --add-port=80/tcp --permanent firewall-cmd --reload
当咱们看到这个界面就意味安装成功了:
建立 service 文件,若是你的安装文件路径和下面的配置文件的路径不一致的时候,须要进行对应的替换。
cat <<EOF >> /usr/lib/systemd/system/nginx1.service [Unit] Description=The nginx HTTP and reverse proxy server After=network.target remote-fs.target nss-lookup.target [Service] Type=forking PIDFile=/run/nginx.pid ExecStartPre=/usr/bin/rm -f /run/nginx.pid ExecStartPre=/usr/sbin/nginx -t ExecStart=/usr/sbin/nginx ExecReload=/bin/kill -s HUP $MAINPID KillSignal=SIGQUIT TimeoutStopSec=5 KillMode=process PrivateTmp=true [Install] WantedBy=multi-user.target EOF
经过 systemd 来管理 nginx
# 查看状态
systemctl status nginx
# 启动nginx
systemctl start nginx
# 暂停nginx
systemctl stop nginx
# 重启nginx
systemctl restart nginx
# 设置开机自启
systemctl enable nginx
# 关闭开机自启
systemctl disable nginx
最后咱们再讲讲几个nginx的命令:
指定配置文件启动:/usr/bin/nginx -c conf/nginx.conf 查看配置文件是否正确:/usr/bin/nginx -t 从新加载配置|重启|中止|退出 nginx : nginx -s reload|reopen|stop|quit 打开帮助信息: -?,-h 查看版本信息: -v 查看版本信息和配置信息: -V
以上就是安装nginx的一个笔记。