欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Linux centos7.7 中安装nginx

程序员文章站 2022-07-12 20:17:32
...

国际惯例,先上图lamp or lnmp不做过多解释。

Linux centos7.7 中安装nginx

环境检查Linux版本,是否安装Apache,是否已经安装nginx

[aaa@qq.com notes]# cat /etc/centos-release
CentOS Linux release 7.7.1908 (Core)
[aaa@qq.com notes]# rpm -q httpd
package httpd is not installed
[aaa@qq.com notes]# rpm -q nginx
package nginx is not installed

###添加Nginx到YUM源,除了太长,没毛病。

[aaa@qq.com notes]# sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
Retrieving http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
warning: /var/tmp/rpm-tmp.PxwEzV: Header V4 RSA/SHA1 Signature, key ID 7bd9bf62: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:nginx-release-centos-7-0.el7.ngx ################################# [100%]

安装nginx虽然已经是root 了,但还是低调的用上了sudo,过程全自动不过多解释。 

这里就不wget下载,tar解压,./configure配置,make编译,make install安装了,有了yum干嘛不享受呢。

人生苦短,我用yum!!!

[aaa@qq.com notes]# sudo yum install -y nginx 
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirror.bit.edu.cn
 * extras: mirror.bit.edu.cn
 * updates: mirror.bit.edu.cn
nginx                                                                                                                                                         | 2.9 kB  00:00:00     
nginx/x86_64/primary_db                                                                                                                                       |  51 kB  00:00:03     
Resolving Dependencies
--> Running transaction check
---> Package nginx.x86_64 1:1.16.1-1.el7.ngx will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=====================================================================================================================================================================================
 Package                                 Arch                                     Version                                              Repository                               Size
=====================================================================================================================================================================================
Installing:
 nginx                                   x86_64                                   1:1.16.1-1.el7.ngx                                   nginx                                   766 k

Transaction Summary
=====================================================================================================================================================================================
Install  1 Package

Total download size: 766 k
Installed size: 2.7 M
Downloading packages:
nginx-1.16.1-1.el7.ngx.x86_64. FAILED                                           [===========================-                                      ]  1.6 B/s | 322 kB  77:56:28 ETA 
http://nginx.org/packages/centos/7/x86_64/RPMS/nginx-1.16.1-1.el7.ngx.x86_64.rpm: [Errno 12] Timeout on http://nginx.org/packages/centos/7/x86_64/RPMS/nginx-1.16.1-1.el7.ngx.x86_64.rpm: (28, 'Operation too slow. Less than 1000 bytes/sec transferred the last 30 seconds')
Trying other mirror.
nginx-1.16.1-1.el7.ngx.x86_64.rpm                                                                                                                             | 766 kB  00:00:10     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Warning: RPMDB altered outside of yum.
  Installing : 1:nginx-1.16.1-1.el7.ngx.x86_64                                                                                                                                   1/1 
----------------------------------------------------------------------

Thanks for using nginx!

Please find the official documentation for nginx here:
* http://nginx.org/en/docs/

Please subscribe to nginx-announce mailing list to get
the most important news about nginx:
* http://nginx.org/en/support.html

Commercial subscriptions for nginx are available on:
* http://nginx.com/products/

----------------------------------------------------------------------
  Verifying  : 1:nginx-1.16.1-1.el7.ngx.x86_64                                                                                                                                   1/1 

Installed:
  nginx.x86_64 1:1.16.1-1.el7.ngx                                                                                                                                                    

Complete!
[aaa@qq.com notes]# 

设置nginx开机自启 

开机启动nginx服务
[aaa@qq.com notes]# sudo systemctl enable nginx.service
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.

为了避免外部不可访问,防火墙操作,开放80端口,并重启防火墙,nginx默认端口是80。

开放80端口
[aaa@qq.com notes]# firewall-cmd --permanent --zone=public --add-port=80/tcp
success


重启防火墙
[aaa@qq.com notes]# firewall-cmd --reload
success


检查防火墙开放端口
[aaa@qq.com notes]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens160
  sources: 
  services: dhcpv6-client ssh vnc-server
  ports: 80/tcp
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 

 


启动nginx服务
[aaa@qq.com notes]# sudo systemctl start nginx.service

 

浏览器登录,尝试访问nginx服务器,最上面的图已经给出来了,那就是最终的效果。
 

其他:

网站文件存放默认目录
/usr/share/nginx/html

网站默认站点配置
/etc/nginx/conf.d/default.conf

自定义Nginx站点配置文件存放目录
/etc/nginx/conf.d/

Nginx全局配置
/etc/nginx/nginx.conf

Nginx启动
nginx -c nginx.conf