centos 7编译安装 nginx服务--最佳实践

1、系统环境以及下载软件安装包:

环境说明:本次使用的为centos 7操作系统 软件版本为:tengine-2.3.2 iptables firewalld已经关闭:

软件下载,可以在阿里云镜像站上进行下载:

http://tengine.taobao.org/download/tengine-2.3.2.tar.gz

或者操作系统上直接下载:

wget http://tengine.taobao.org/download/tengine-2.3.2.tar.gz

下载完成之后,将软件包存放在指定的位置上,然后进行解压操作

2、进行编译安装:假如安装路径为:/usr/local/services/tengine-2.3.2

源文件安装包为:/usr/local/services/src/tengine-2.3.2.tar.gz

进行解压操作:cd /usr/local/services/src/

[root@localhost src]# tar  -zxvf tengine-2.3.2.tar.gz 

解压到当前路径 然后生成目录文件夹:/usr/local/services/src/tengine-2.3.2

解压完成之后进行编译安装,安装模块信息如下

[root@localhost src]# cd tengine-2.3.2/
[root@localhost tengine-2.3.2]#
./configure --prefix=/usr/local/services/tengine-2.3.2 --user=www --group=www --with-http_ssl_module --with-http_lua_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-sha1=/usr --with-stream --with-openssl=/usr/local/services/src/openssl-1.1.1d --with-md5=/usr/local/services/src/md5 --with-pcre=/usr/local/services/src/pcre-8.42 --with-luajit-inc=/usr/local/services/include/luajit-2.1 --with-luajit-lib=/usr/local/services/lib --without-select_module --without-poll_module --without-http_userid_module --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module --without-http_upstream_keepalive_module --add-module=modules/ngx_slab_stat --add-module=modules/ngx_http_reqstat_module --add-module=modules/ngx_http_upstream_* --add-module=../ngx_cache_purge-2.3

因为安装过程需要调用相关的模块信息,因此做以下前期环境准备,如不需要相关模块可以删除:

前期环境准备:

系统服务器上创建NGINX服务启动需要的用户及用户组信息:此处为www

方法1:创建用户组信息:系统创建默认用户及用户组信息
useradd www

方法2
groupadd www
创建用户以及指定UID和家目录
useradd -g www -u 1100 -d /home/www www
参数说明,以下为NGINX时安装指定的模块,如果不需要可以不安装:
--prefix=/usr/local/services/tengine-2.3.2表示指定的软件安装路径(可以自定义)
--with-openssl=/usr/local/services/src/openssl-1.1.1d(需要先将OpenSSL软件包解压后放到该路径下)
 以及pcre
--with-pcre=/usr/local/services/src/pcre-8.42
--add-module=../ngx_cache_purge-2.3
 --with-luajit-inc=/usr/local/services/include/luajit-2.1 (该模块需要先安装后调用)

pcre-8.42软件包 下载完放置到指定路径
https://jaist.dl.sourceforge.net/project/pcre/pcre/8.42/pcre-8.42.tar.gzpcre-8.42软件包 下载完放置到指定路径

ngx_cache_purge-2.3软件包

http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz

LuaJIT-2.1.0-beta3软件包:

https://github.com/LuaJIT/LuaJIT/archive/v2.1.0-beta3.tar.gz

或者自行下载需要的版本即可

解压后的文件如图所示:

编译安装luajit-2.1:解压完成之后进入文件夹

[root@localhost LuaJIT-2.1.0-beta3]# ll
total 32
-rw-r--r--. 1 elasticsearch elasticsearch 2932 May  2  2017 COPYRIGHT
drwxr-xr-x. 3 elasticsearch elasticsearch 4096 May  2  2017 doc
drwxr-xr-x. 2 elasticsearch elasticsearch  283 May  2  2017 dynasm
drwxr-xr-x. 2 elasticsearch elasticsearch   39 May  2  2017 etc
-rw-r--r--. 1 elasticsearch elasticsearch 5879 May  2  2017 Makefile
-rw-r--r--. 1 elasticsearch elasticsearch  453 May  2  2017 README
drwxr-xr-x. 4 elasticsearch elasticsearch 8192 May  2  2017 src
[root@localhost LuaJIT-2.1.0-beta3]# make install PREFIX=/usr/local/services/
==== Building LuaJIT 2.1.0-beta3 ====
make -C src
...
==== Successfully installed LuaJIT 2.1.0-beta3 to /usr/local/services/ ====

Note: the development releases deliberately do NOT install a symlink for luajit
You can do this now by running this command (with sudo):

  ln -sf luajit-2.1.0-beta3 /usr/local/services//bin/luajit

[root@localhost LuaJIT-2.1.0-beta3]# 
=====================表示安装完成=======================

如果安装完成之后 路径有问题的可以将安装的文件移动放置到定义的路径即可

 

进入到该目录下进行编译安装:

前期环境部署完成之后,进入到tengine-2.3.2/解压目录进行编译安装

[root@localhost src]# cd tengine-2.3.2/
[root@localhost tengine-2.3.2]#
./configure --prefix=/usr/local/services/tengine-2.3.2 --user=www --group=www --with-http_ssl_module --with-http_lua_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-sha1=/usr --with-stream --with-openssl=/usr/local/services/src/openssl-1.1.1d --with-md5=/usr/local/services/src/md5 --with-pcre=/usr/local/services/src/pcre-8.42 --with-luajit-inc=/usr/local/services/include/luajit-2.1 --with-luajit-lib=/usr/local/services/lib --without-select_module --without-poll_module --without-http_userid_module --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module --without-http_upstream_keepalive_module --add-module=modules/ngx_slab_stat --add-module=modules/ngx_http_reqstat_module --add-module=modules/ngx_http_upstream_* --add-module=../ngx_cache_purge-2.3

然后执行上述编译安装过程,如果提示相关依赖缺少的情况,可以使用yum 方式安装即可

Configuration summary
  + using system PCRE library
  + using system OpenSSL library
  + using system zlib library
  + using system jemalloc library
 
  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"

直到出现这个信息的时候就可以进行make  &  make install了

在没有报错之后进行make 和make install

[root@localhost tengine-2.3.2]# make
......
make[1]: Leaving directory '/usr/local/services/tengine-2.3.2'
# 这里的make[1]直接忽略,然后执行make install
 
[root@localhost tengine-2.3.2]# make install
# 这里很快就执行完了,然后需要查看安装路径"/usr/local/services/tengine-2.3.2"存不存在,如果存在则编译成功,失败则编译失败
 

配置system启动:

[root@localhost tengine-2.3.2]# systemctl  cat nginx.service 
# /usr/lib/systemd/system/nginx.service
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/services/tengine-2.3.2/logs/nginx.pid
ExecStartPre=/usr/local/services/tengine-2.3.2/sbin/nginx -t
ExecStart=/usr/local/services/tengine-2.3.2/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

User=www
Group=www

[Install]
WantedBy=multi-user.target
[root@localhost tengine-2.3.2]# 

配置环境变量:

[root@localhost tengine-2.3.2]# cat /etc/profile.d/nginx.sh 
export NGINX_HOME="/usr/local/services/tengine-2.3.2"
export PATH="$NGINX_HOME/sbin:$PATH"
[root@localhost tengine-2.3.2]# source /etc/profile.d/nginx.sh 

执行测试操作

[root@localhost profile.d]# nginx  -t
nginx: the configuration file /usr/local/services/tengine-2.3.2/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/services/tengine-2.3.2/conf/nginx.conf test is successful
[root@localhost profile.d]# 

如果执行命令过程中提示libluajit-5.1.so.2不存在的情况则需要创建软连接然后再次测试

 ln -s /usr/local/services/lib/libluajit-5.1.so.2 /lib64/

执行过程提示权限用户信息的,需要将NGINX的配置文件,调用的文件夹、启动的用户信息等修改为安装时执行的文件:

如果用户不匹配将无法正常启动

(启动用户可以在system配置文件中启动 User=www Group=www)

修改完用户已经文件所有者之后再次启动

启动服务过程中出现0.0.0.0:80端口问题的 解决方式:

原因:当前用户对该位置没有写入权限
解决办法:
1.使用命令:sudo /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf 以root权限启动
2.使用命令:sudo chmod -R a+rw /usr/local/nginx 给所有用户赋权限(个人学习,不考虑安全问题)
                    /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf  启动Nginx

注:以非root权限启动时,会出现 nginx: [emerg] bind() to 0.0.0.0:80 failed (13: Permission denied) 错误
原因:Linux只有root用户可以使用1024一下的端口
解决办法:1.已root权限启动
  2.将 /usr/local/nginx/conf/nginx.conf 文件中的80端口改为1024以上
server {
# listen 80
   listen 8080
……
}

根据需要,配置NGINX加密访问

为了可以设置某些网站(虚拟主机的访问控制),可以对网站进行加密认证,进行简单的配置为使用OpenSSL/htpasswd方式进行加密:

要想实现Nginx密码访问不难,通过htpasswd即可轻松实现,以Centos7系统为例,安装htpasswd命令如下:
### centos yum -y install httpd-tools  
### debian/ubuntu apt-get install apache2-utils -y

#进入指定目录(自定义) 
cd /data/fans/opt/
#生成密码 
htpasswd -c ./.nginx.passwd  username #执行上命令后会要求输入两次密码,./.nginx.passwd; 是在当前目录下创建密码文件passwd  
username为指定可以登录的账户

Nginx配置通过账号密码访问:
编辑nginx站点配置文件,在 Server段内添加以下语法,然后重启nginx服务 (/usr/local/nginx/conf/nginx.conf(NGINX的配置文件实际路径))

在server模块下进行配置:需要进行验证的虚拟主机上添加即可
....
   server {
        listen       8888;
        server_name  localhost;
        auth_basic "Please input password";   #这里是验证时的提示信息
        auth_basic_user_file /data/fans/opt/.nginx.passwd;
        #charset koi8-r;
...
注意事项:配置注意分好 ;

使用OpenSSL方式进行配置:

1、创建用户名为elkuser
echo -n ‘elkuser:’ > /data/fans/opt/.nginx.passwd
2、创建用户密码
openssl passwd 123456 >>  /data/fans/opt/.nginx.passwd
 
Nginx配置通过账号密码访问:
编辑nginx站点配置文件,在 Server段内添加以下语法,然后重启nginx服务 (/usr/local/nginx/conf/nginx.conf(NGINX的配置文件实际路径))

在server模块下进行配置:需要进行验证的虚拟主机上添加即可
....
   server {
        listen       8888;
        server_name  localhost;
        auth_basic "Please input password";   #这里是验证时的提示信息
        auth_basic_user_file /data/fans/opt/.nginx.passwd;
        #charset koi8-r;
...
注意事项:配置注意分好 ;

配置完成之后可以进行reload测试

systemctrl reload nginx

测试效果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值