Linux安装Nginx
环境
SystemOS:CentOS 7
相关插件下载(可选)
yum -y install gcc
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。 nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库
yum install -y openssl openssl-devel
安装Nginx
# 如果没有安装wget 可以执行 yum install wget 安装
wget http://nginx.org/download/nginx-1.20.0.tar.gz
# 实际看自己下的哪个版本
tar -zxvf nginx-1.20.0.tar.gz
# 先进入解压后的Nginx文件夹
cd nginx-1.20.0
- 使用默认配置(推荐,默认配置在/usr/local/nginx目录下)
./configure
- 自定义配置(只是配置(配置安装路径),不会安装)
# 注意:将临时文件目录指定为/var/temp/nginx,需要在/var下创建temp及nginx目录
./configure \
--prefix=/usr/local/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--pid-path=/usr/local/nginx/conf/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi
# 在nginx解压后文件目录内执行
make && make install
1. Nginx默认安装在/usr/local/nginx,安装完后有需要的话可以把解压文件删了
cd /usr/local/nginx/sbin/
1. 启动Nginx
./nginx
1. 此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程
./nginx -s stop
1. 此方式停止步骤是待nginx进程处理任务完毕进行停止
./nginx -s quit
1. 在Nginx启动状态下,重启Nginx
./nginx -s reload
vi /etc/rc.local
1. 增加一行
/usr/local/nginx/sbin/nginx
1. 设置执行权限
chmod 755 rc.local
配置Nginx为服务&设置开机启动(需要把上面设置的开机启动取消)
cd /lib/systemd/system/
vim nginx.service
/usr/local/nginx
)[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
1. 路径对应安装路径
Type=forking
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
chmod -r 755 ./nginx.service
systemctl start nginx.service 启动nginx
systemctl stop nginx.service 结束nginx
systemctl restart nginx.service 重启nginx
# 设置开机启动
systemctl enable nginx.service
1. 这句我也不知道啥意思。。有的教程有写有的没有
systemctl daemon-reload
export NGINX_HOME=/usr/local/nginx
export PATH=${NGINX_HOME}/sbin:${PATH}
3. 生效
source /etc/profile
4. 验证
nginx -v
验证配置是否正确: nginx -t
查看Nginx的详细的版本号:nginx -V
查看Nginx的简洁版本号:nginx -v
启动Nginx:start nginx
快速停止或关闭Nginx:nginx -s stop
正常停止或关闭Nginx:nginx -s quit
配置文件修改重装载命令:nginx -s reload
配置nginx.conf
相关问题
- 启动时报80端口被占用: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
- 解决方案:
- 安装net-tool包:
yum install net-tools
- 查询进程:
ps aux|grep nginx