centos中nginx使用

一、nginx是什么?

Nginx 是一个高性能的HTTP和反向代理web服务器,他是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,在BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。

二、使用步骤

1.配置yum源

配置yum源有2种方式,一种是网络yum,一种是本地yum
本地yum配置

mount /dev/sr0 /mnt/ //将包挂载在mnt目录下
cd /etc/yum.repos.d/
rm -rf *   //进入yum中删除所有
vim yum.repo //创建并且编辑文件如下
[centos]
name=centos
baseurl=file:///mnt  //挂载位置
gpgcheck=0 //用来检查GPG-KEY,0为不检查,1为检查
enabled=1 //是否用该yum源,0为禁用,1为使用

yum clean all //清理缓存
yum repolist //查看包的个数

网络yum

http://mirrors.aliyun.com/repo/ //进入这个网站找合适的包
cd /etc/yum.repos.d/
rm -rf *   //进入yum.repos.d中删除所有
wget -O /etc/yum.repos.d/base.repo http://mirrors.aliyun.com/repo/Centos-7.repo //网上找个centos7的包,下载到yum.repos.d中
yum clean all //清除缓存
yum makecache //生成新的缓存
yum repolist //查看包的个数

2.yum安装支持nginx各种软件包

yum -y install  gcc  make  //安装编译工具
yum -y install  pcre-devel  //安装可以让nginx支持正则的软件包
yum -y install  openssl-devel  //安装可以让nginx支持安装加密网站的软件包

3.nginx安装

wget http://nginx.org/download/nginx-1.17.6.tar.gz //下载nginx包
cd nginx-1.17.6/    //进入nginx目录
./configure  --prefix=/usr/local/nginx  --user=nginx  --with-http_ssl_module  //配置,--prefix是指定安装路径,--user是指定用户  --with-http_ssl_module是安全网站模块
make   //编译
make  install  //安装			
开启服务并测试
cd /usr/local/nginx
useradd  nginx  -s  /sbin/nologin       //创建用户,因为root权限太大,上面就是指定nginx目录
cd /usr/local/nginx
sbin/nginx		//开启服务也可以
sbin/nginx  -V   //查看nginx版本以及安装时带了哪些参数和模块
systemctl  stop  firewalld   //关闭防火墙
真机使用浏览器输入你的ip地址即可访问nginx页面
sbin/nginx  -s  stop   //关闭服务
sbin/nginx  -s  reload  //重加载配置文件

4.编写Unit文件,使systemctl命令控制nginx

正常来说使用systemctl start nginx 是不能开启nginx的
我们可以做一些小配置

cd /usr/lib/systemd/system
cp httpd.service nginx.service
vim nginx.service
//源配置
[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target
Documentation=man:httpd(8)
Documentation=man:apachectl(8)
[Service]
Type=notify
EnvironmentFile=/etc/sysconfig/httpd
ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND
ExecReload=/usr/sbin/httpd $OPTIONS -k graceful
ExecStop=/bin/kill -WINCH ${MAINPID}
# We want systemd to give httpd some time to finish gracefully, but still want
# it to kill httpd after TimeoutStopSec if something went wrong during the
# graceful stop. Normally, Systemd sends SIGTERM signal right after the
# ExecStop, which would kill httpd. We are sending useless SIGCONT here to give
# httpd time to finish.
KillSignal=SIGCONT
PrivateTmp=true
[Install]
WantedBy=multi-user.target

//改变后配置
[Unit]
Description=The nginx HTTP Server  //描述
After=network.target remote-fs.target nss-lookup.target  //在网络服务、网络文件服务、域名服务的程序启动之后再启动nginx
[Service]
Type=forking  //nginx是多进程类型程序,要设置为forking
ExecStart=/usr/local/nginx/sbin/nginx  //当执行了systemctl start nginx之后执行的命令
ExecReload=/usr/local/nginx/sbin/nginx -s reload   //当执行了systemctl reload nginx之后执行的命令
ExecStop=/usr/local/nginx/sbin/nginx -s  stop //当执行了systemctl stop nginx之后执行的命令
[Install]
WantedBy=multi-user.target   //支持开机自启

之后就可以用systemctl 命令呢
[root@test system]# systemctl start nginx
[root@test system]# systemctl status nginx.service 
● nginx.service - The nginx HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: active (running) since 一 2022-06-27 20:32:55 CST; 8s ago
  Process: 32527 ExecStart=/usr/local/nginx/sbin/nginx (code=exited, status=0/SUCCESS)
 Main PID: 32528 (nginx)
   CGroup: /system.slice/nginx.service
           ├─32528 nginx: master process /usr/local/nginx/sbin/nginx
           └─32529 nginx: worker process

627 20:32:55 test systemd[1]: Starting The nginx HTTP Server...
627 20:32:55 test systemd[1]: Started The nginx HTTP Server.
[root@test system]# ^C
[root@test system]# systemctl stop nginx.service     
[root@test system]# systemctl status nginx.service 
● nginx.service - The nginx HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: inactive (dead)

627 20:32:55 test systemd[1]: Starting The nginx HTTP Server...
627 20:32:55 test systemd[1]: Started The nginx HTTP Server.
627 20:34:00 test systemd[1]: Stopping The nginx HTTP Server...
627 20:34:00 test systemd[1]: Stopped The nginx HTTP Server.

总结

这是我第一次写博客,有什么不足的地方望请大家指出。接下来我会和大家一起学习,并且会与大家分析更多nginx操作。

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值