方法一、在centos7安装nginx
wget http://nginx.org/download/nginx-1.13.1.tar.gz
解压:
tar -zxvf nginx-1.13.1.tar.gz
移动到:
cd /nginx-1.13.1
安装
./configure --prefix=/usr/local/nginx && make && make install
如果出现这个错误:
请先安装
yum -y install pcre-devel
安装成功后可以使用/usr/local/nginx/sbin/nginx命令启动nginx,这样启动太麻烦了,所以配置systemctl启动,添加文件:
vim /usr/lib/systemd/system/nginx.service
文件输入内容为
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
WorkingDirectory=/usr/local/nginx
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
让文件生效
systemctl daemon-reload
这次可以通过
systemctl start nginx #启动
systemctl stop nginx #关闭
这样的话nginx命令依然不能为全局变量,还是需要配置
vim /etc/profile
在文件最后一行输入
export PATH=$PATH:/usr/local/nginx/sbin
使文件生效
source /etc/profile
好了,这次可以使用nginx命令了
方法二、使用yum命令
添加yum源
sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
使用命令安装
yum install nginx
搞定
启动命令
sudo systemctl start nginx