LMNP 之 Nginx

当前系统是最小化安装的 Centos

[root@localhost ~]# free -h
              total        used        free      shared  buff/cache   available
Mem:           3.7G        116M        3.4G        8.8M        169M        3.4G
Swap:          2.0G          0B        2.0G
[root@localhost ~]# df -h
文件系统                      容量  已用  可用 已用% 挂载点
/dev/mapper/VolGroup-lv_root   50G  1.5G   46G    4% /
devtmpfs                      1.9G     0  1.9G    0% /dev
tmpfs                         1.9G     0  1.9G    0% /dev/shm
tmpfs                         1.9G  8.9M  1.9G    1% /run
tmpfs                         1.9G     0  1.9G    0% /sys/fs/cgroup
/dev/sda1                     477M  114M  334M   26% /boot
/dev/mapper/VolGroup-lv_home   12G   41M   11G    1% /home
Home                          239G   55G  184G   23% /media/psf/Home
iCloud                        239G   55G  184G   23% /media/psf/iCloud
iMac.hk                       464M  394M   71M   85% /media/psf/iMac.hk
tmpfs                         379M     0  379M    0% /run/user/0
[root@localhost ~]#

下载 Nginx 最新稳定版本:

[root@localhost ~]# mkdir /software
[root@localhost ~]# cd /software/
[root@localhost software]#
[root@localhost software]# wget http://nginx.org/download/nginx-1.16.0.tar.gz
--2019-04-25 22:50:16--  http://nginx.org/download/nginx-1.16.0.tar.gz
正在解析主机 nginx.org (nginx.org)... 95.211.80.227, 62.210.92.35, 2001:1af8:4060:a004:21::e3
正在连接 nginx.org (nginx.org)|95.211.80.227|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:1032345 (1008K) [application/octet-stream]
正在保存至: “nginx-1.16.0.tar.gz”

100%[=====================================================>] 1,032,345   12.5KB/s 用时 79s

2019-04-25 22:51:36 (12.7 KB/s) - 已保存 “nginx-1.16.0.tar.gz” [1032345/1032345])

[root@localhost software]#
[root@localhost software]# tar -zxf nginx-1.16.0.tar.gz
[root@localhost software]#

安装依赖

[root@localhost software]# yum install -y gcc-c++ gcc libxml2-devel libxslt-devel perl-devel perl-ExtUtils-Embed pcre pcre-devel openssl openssl-devel

创建 nginx 执行用户

[root@localhost software]# groupadd -r www && useradd -r -g www -s /bin/false -M www
[root@localhost software]#

编译

[root@localhost software]# cd nginx-1.16.0
[root@localhost nginx-1.16.0]#
[root@localhost nginx-1.16.0]# ./configure \
> --prefix=/usr/local/nginx \
> --sbin-path=/usr/local/nginx/sbin/nginx \
> --conf-path=/usr/local/nginx/conf/nginx.conf \
> --error-log-path=/usr/local/nginx/logs/error.log \
> --http-log-path=/usr/local/nginx/logs/access.log \
> --pid-path=/usr/local/nginx/logs/nginx.pid  \
> --user=www \
> --group=www \
> --with-http_ssl_module \
> --with-http_v2_module \
> --with-http_dav_module \
> --with-http_flv_module \
> --with-http_realip_module \
> --with-http_addition_module \
> --with-http_xslt_module \
> --with-http_stub_status_module \
> --with-http_sub_module \
> --with-http_random_index_module \
> --with-http_degradation_module \
> --with-http_secure_link_module \
> --with-http_gzip_static_module \
> --with-http_perl_module \
> --with-file-aio \
> --with-mail \
> --with-mail_ssl_module \
> --with-ld-opt="-Wl,-E"

安装,安装前要检查下编译是否有错误,确认 echo $? 输出 0

[root@localhost nginx-1.16.0]# echo $?
0
[root@localhost nginx-1.16.0]#
[root@localhost nginx-1.16.0]# make && make install

配置启动项

[root@localhost nginx-1.16.0]# vi /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
PIDFile=/usr/local/nginx/logs/nginx.pid
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=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target
[root@localhost nginx-1.16.0]# 

加入开机自启动

[root@localhost nginx-1.16.0]# systemctl enable nginx.service
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
[root@localhost nginx-1.16.0]#

启动 nginx 服务

[root@localhost nginx-1.16.0]# systemctl start nginx.service
[root@localhost nginx-1.16.0]# systemctl status nginx.service
● nginx.service - nginx - high performance web server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since 四 2019-04-25 23:15:18 CST; 3s ago
     Docs: http://nginx.org/en/docs/
  Process: 9665 ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf (code=exited, status=0/SUCCESS)
  Process: 9661 ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf (code=exited, status=0/SUCCESS)
 Main PID: 9666 (nginx)
   CGroup: /system.slice/nginx.service
           ├─9666 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf...
           └─9667 nginx: worker process

4月 25 23:15:18 localhost.localdomain systemd[1]: Starting nginx - high performance web s.....
4月 25 23:15:18 localhost.localdomain nginx[9661]: nginx: the configuration file /usr/loc...ok
4月 25 23:15:18 localhost.localdomain nginx[9661]: nginx: configuration file /usr/local/n...ul
4月 25 23:15:18 localhost.localdomain systemd[1]: Started nginx - high performance web server.
Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost nginx-1.16.0]#

服务启动后,通过 IP 访问测试,提示被拒绝访问,首先排查下防火墙

[root@localhost nginx-1.16.0]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: active (running) since 四 2019-04-25 22:27:02 CST; 49min ago
     Docs: man:firewalld(1)
 Main PID: 3650 (firewalld)
   CGroup: /system.slice/firewalld.service
           └─3650 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid

4月 25 22:27:02 localhost.localdomain systemd[1]: Starting firewalld - dynamic firewall d.....
4月 25 22:27:02 localhost.localdomain systemd[1]: Started firewalld - dynamic firewall daemon.
Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost nginx-1.16.0]# 
[root@localhost nginx-1.16.0]# firewall-cmd --query-port=80/tcp
no
[root@localhost nginx-1.16.0]#

发现防火墙是开启的,并且没有开放80端口,

[root@localhost nginx-1.16.0]# firewall-cmd --permanent --zone=public --add-port=80/tcp
success
[root@localhost nginx-1.16.0]# firewall-cmd --query-port=80/tcp
no
[root@localhost nginx-1.16.0]# systemctl restart firewalld
[root@localhost nginx-1.16.0]# firewall-cmd --query-port=80/tcp
yes
[root@localhost nginx-1.16.0]#

再次访问正常。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值