Linux下nginx的源码安装和配置
程序员文章站
2022-05-17 13:57:48
...
- 实验环境:一台虚拟主机 rhel7.6
安装nginx
-
安装nginx依赖包
yum install -y pcre-devel zlib-devel openssl-devel wget gcc tree vim
-
Nginx依赖于pcre、zlib、openssl,在编译前配置时如果有问题,可以使用yum方式安装三个包(pcre-devel、zlib-devel、openssl-devel)
-
从Nginx官网下载Nginx源码包
wget http://nginx.org/download/nginx-1.18.0.tar.gz
-
解压Nginx源码包到/nginx/(自己创建的目录),并查看Nginx源文件结构
-
tar -xzvf nginx-1.18.0.tar.gz
-
进行编译前配置
- cd /nginx/nginx-1.18.0/
- ./configure --prefix=/usr/local/nginx --with-http_ssl_module
- 在/root/nginx目录执行编译安装
make && make install
启动nginx
- 关闭防火墙
setenforce 0
systemctl stop firewalld
systemctl disable firewalld
-
进入到安装目录
/usr/local/nginx
,查看目录结构
- -
查看Nginx占用的端口号
netstat -tlnp
-
查看是否打开
netstat -antlp | grep :80
停止nginx
- 停止Nginx的三种方式
-
立即停止Nginx服务
/usr/local/nginx/sbin/nginx -s stop
-
完成当前任务后停止
/usr/local/nginx/sbin/nginx -s quit
-
杀死Nginx进程
killall nginx
把nginx命令添加到环境变量
- 使用软连接将nginx连接到/usr/local/sbin
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin
ll /usr/local/sbin/ | grep "nginx"
- 显示当前环境变量
-
echo $PATH
- 编辑.bash_profile文件
vim ~/.bash_profile
- 在.bash_profile文件末尾加入以下内容
export PATH=$PATH:/usr/local/nginx/sbin
source ~/.bash_profile
- 使用nginx命令
nginx##启动nginx
nginx -s quit ##停止nginx
把nginx命令添加到系统服务
- cd /usr/lib/systemd/system/
- 在此目录下新建nginx.service文件
vim nginx.service
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
vim /etc/security/limits.conf
nginx - nofile 65535
源码方式安装nginx自动化安装脚本
- vim nginx_auto.sh
#!/bin/bash
#configuration
NGINX_VERSION=1.18.0
mkdir /nginx
NGINX_SRC_PATH=/nginx
NGINX_BIN_PATH=/usr/local/nginx
#diable firewalls
systemctl stop firewalld
setenforce 0
#installion dependence
yum install -y pcre-devel zlib-devel openssl-devel wget gcc
#download nginx source package
cd ${NGINX_SRC_PATH}
wget http://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz
#unzip source package
tar -xzvf nginx-${NGINX_VERSION}.tar.gz
cd ./nginx-${NGINX_VERSION}
#install nginx
./configure --prefix=${NGINX_BIN_PATH} --with-http_ssl_module
make & make install
# start nginx service
cd ${NGINX_BIN_PATH}/sbin
./nginx
#END
上一篇: CentOS7.0安装配置Storm集群
下一篇: 每日一搏 | php 绘制图片验证码