1.更新gcc安装包
#如果是linux
yum install -y gcc pcre-devel zlib-devel
#如果是ubuntu
sudo apt install build-essential gcc automake
sudo apt-get install libpcre3 libpcre3-dev
sudo apt-get install openssl libssl-dev
sudo apt install zlib1g zlib1g-dev
2.下载文件并解压
命令如下
wget https://nginx.org/download/nginx-1.18.0.tar.gz
//解压nginx
tar -zxvf nginx-1.18.0.tar.gz
cd nginx-1.18.0
//进入后进行指定安装位置
./configure --prefix=/usr/local/nginx
//安装
make && make install
//启动nginx
cd /usr/local/nginx/sbin
//启动命令
./nginx
//重启命令
./nginx -s reload
//关闭命令
/nginx -s stop
如果想在任何地方执行nginx命令,而不是进入sbin目录,可以增加环境变量信息
vim /etc/profile
#最后一行加上 只配置nginx
export PATH=$PATH:/usr/local/nginx/sbin
#同时配置mysql和nginx
export PATH=$PATH:/usr/local/mysql/bin:/usr/local/nginx/sbin
#刷新权限
source /etc/profile
3.开机自启动
1. 创建启动服务。
/usr/lib/systemd/system/目录下创建nginx.service
ExecStartPre、ExecStart 、ExecReload等nginx地址 为自己的安装地址 这里我安装在/usr/local/nginx
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target[Service]
Type=forking
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
2. 设置开启自启动
systemctl daemon-reload
systemctl enable nginx.service
报异常信息 未权限
Failed to start nginx.service: Access denied
See system logs and 'systemctl status nginx.service' for details
执行命令
setenforce 0
配置成功
3. 启动服务
systemctl start nginx
4.查看服务状态
systemctl status nginx