Nginx的几种安装方式

举报
灰烬 发表于 2022/02/13 17:43:22 2022/02/13
【摘要】 Nginx是一个开源且高性能、可靠的HTTP中间件、代理服务

=================================

/*

* @平台 | 系统:华为云 | CentOS7

* @web:nginx-1.20.2

* @描述:nginx 的yum安装以及编译安装

*@注意:注意安全组以及防火墙端口开放

*/

=================================

一、yum安装

# vim /etc/yum.repos.d/nginx.repo
# ===content===
[nginx-stable]
name=nginx stable
repobaseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
# ===end===
# yum makecache
# 安装zlib用以解决压缩和解压缩依赖
# 安装pcre库用以解析正则表达式
# 安装openssl用以http安全传输
# yum install -y zlib zlib-devel pcre pcre-devel openssl openssl-devel
# yum install -y nginx
# nginx -v
nginx version: nginx/1.20.2
# 设置开机自启并启动服务
# systemctl enable --now nginx
# netstat -antup |grep nginx
tcp   0   0 0.0.0.0:80   0.0.0.0:*   LISTEN   9672/nginx: master
# 开放端口80 | 443
# firewall-cmd --zone=public --add-port=80/tcp --permanent
# firewall-cmd --zone=public --add-port=443/tcp --permanent
# firewall-cmd --relaod
# curl -I localhost
HTTP/1.1 200 OK
# 通过浏览器访问正常(公网IP/域名)
# =======
Welcome to nginx!If you see this page, the nginx web server is successfully installed and working. Further configuration is required.​

For online documentation and support please refer to nginx.org.Commercial support is available at nginx.com.​

Thank you for using nginx.

二、编译安装

# 创建安装包存放位置
# mkdir /home/softs
# 创建网站根目录
# mkdir /home/www
# cd /home/softs/
# wget https://nginx.org/download/nginx-1.20.2.tar.gz
# 安装依赖包
# yum install -y perl-devel libxslt-devel libxml2-devel gd-devel GeoIP-devel zlib-devel pcre-devel openssl-devel
# 解压
# tar -zxvf nginx-1.20.2.tar.gz
# 进入到解压出来的目录
# cd nginx-1.20.2
# 编译之前,先创建nginx临时目录,如果不创建,在启动nginx的过程中可能会报错
# mkdir -p /var/tmp/nginx
# 可以根据实际情况进行编译安装,不一定以下都要编译
# ./configure --prefix=/etc/nginx \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--http-client-body-temp-path=/var/tmp/nginx/client \
--http-proxy-temp-path=/var/tmp/nginx/proxy \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi \
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
--http-scgi-temp-path=/var/tmp/nginx/scgi \
--user=nginx --group=nginx \
--with-pcre --with-http_v2_module \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-http_auth_request_module \
--with-mail --with-mail_ssl_module \
--with-file-aio \
--with-ipv6 \
--with-http_v2_module \
--with-threads \
--with-stream \
--with-stream_ssl_module
# make && make install
# 创建组和用户
# groupadd -r nginx
# useradd -r -g nginx -s /sbin/nologin nginx
# 授权用户
# chown -R nginx:nginx /etc/nginx/
# 启动nginx服务
# /usr/sbin/nginx
# curl -I localhost
HTTP/1.1 200 OK
# netstat -antup |grep nginx
tcp   0   0 0.0.0.0:80   0.0.0.0:*   LISTEN   11494/nginx: master
# 停止服务
# cd /usr/sbin/
# ./nginx -s stop
# 创建nginx服务的启动文件
# vim /usr/lib/systemd/system/nginx.service
# ===content===
[Unit]
Description=nginx - high performance web server
Documentation=https://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target 
​
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID 
​
[Install] 
WantedBy=multi-user.target
# ===end===
# 启动服务
# systemctl start nginx
# 查看服务状态
 systemctl status nginx
 Active: active (running)
 # 通过浏览器访问(公网IP或者域名)
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
​
For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.
​
Thank you for using nginx.
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。