linux上部署nginx
一.环境准备
1.确认centos的内核
准备一个内核为2.6及以上版本的操作系统,因为linux2.6及以上内核才支持epoll,而Nginx需要解决高并发压力问题是需要用到epoll,所以我们需要有这样的版本要求。我们可以使用uname -a
命令来查询linux的内核版本
2.确保centos能联网
ping www.baidu.com
3.确认防火墙状态
是否关闭,根据自己的情况来
systemctl stop firewalld 关闭运行的防火墙,系统重新启动后,防火墙将重新打开
systemctl disable firewalld 永久关闭防火墙,,系统重新启动后,防火墙依然关闭
systemctl status firewalld 查看防火墙状态
二.开始安装
1.提前安装nginx可能用到的插件
GCC编译器 (编译工具)
yum install -y gcc 安装
gcc --version 查看gcc是否安装成功
PCRE (正则表达式语法)
yum install -y pcre pcre-devel 安装
rpm -qa pcre pcre-devel 查看是否安装成功
zlib (需要使用gzip压缩时)
yum install -y zlib zlib-devel 安装
rpm -qa zlib zlib-devel 查看是否安装成功
OpenSSL (提供安全网页时就需要用到OpenSSL库)
yum install -y openssl openssl-devel 安装
rpm -qa openssl openssl-devel 查看是否安装成功
上述命令,一个个来的话比较麻烦,我们也可以通过一条命令来进行安装
yum install -y gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel
进行全部安装。
2.nginx下载安装
下载:(我一般把包安在 /usr/loca 下)
wget http://nginx.org/download/nginx-1.16.1.tar.gz
解压缩:
tar -xzf nginx-1.16.1.tar.gz
执行configure: (进入nginx解压完的文件, cd nginx)
./configure
编译:
make
安装
make install
三. 将nginx配成系统服务
配成使用systemctl 可以使用的
1.在/usr/lib/systemd/system
目录下添加nginx.service,内容如下:
vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx web service
Documentation=http://nginx.org/en/docs/
After=network.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
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=default.target
2.添加完成后如果权限有问题需要进行权限设置
chmod 755 /usr/lib/systemd/system/nginx.service
3.使用系统命令来操作Nginx服务
启动: systemctl start nginx
停止: systemctl stop nginx
重启: systemctl restart nginx
重新加载配置文件: systemctl reload nginx
查看nginx状态: systemctl status nginx
开机启动: systemctl enable nginx
四. nginx命令配置到系统环境
(1)修改/etc/profile
文件
vim /etc/profile
在最后一行添加
export PATH=$PATH:/usr/local/nginx/sbin
(2)使之立即生效
source /etc/profile
(3)执行nginx命令
nginx -V
完毕!
补一下:
当我启动nginx时报了:
Nginx: [emerg] the “ssl“ parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf
出现上面标题中的问题是因为我当初在安装Nginx的时候没有安装SSL模块,但是现在我在Nginx配置文件(nginx.cnf)中配置了SSL的相关配置信息。当我再次启动Nginx的时候就提示我nginx: [emerg] the “ssl” parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf了。
解决方案:
1、查看Nginx 的版本
[root@xxx ~]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.16.1
…
configure arguments:
可以看到,这里的configure arguments: 后面没有任何配置参数。
我们现在切换到nginx一开始tar包解压完的目录下面,有.configure 的哪个,增加SSL配置项进行重新编译:
新增SSL模块进行重新配置:
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
重新编译:
make
注意:这里不在需要执行make install命令了,如果执行了会覆盖之前的安装,会有一定的风险哦!
2、具体操作
(1)备份原有已安装好的 nginx :
cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
(2)停止现有的 Nginx 服务:
/usr/local/nginx/sbin/nginx -s stop
(3)将刚刚编译好的 nginx 覆盖掉原有的 nginx,刚才重新编译好的nginx在/usr/local/nginx-1.16.1/objs/ 下面。
cp ./objs/nginx /usr/local/nginx/sbin/
执行的时候会有个询问过程,询问你是否选择覆盖?此时输入 yes
然后按Enter
就行了。
(4)上述执行成功后,再执行查看 Nginx 版本就可以看到configure arguments: 后面有新的参数了,如下:
/usr/local/nginx/sbin/nginx -V 执行
nginx version: nginx/1.16.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
通过上面控制台返回的信息可见新增的SSL模块成功了。
(5)重新启动Nginx就可以啦:
启动Nginx服务
/usr/local/nginx/sbin/nginx