测试
检查是否打开了TCP端口80或443:
$ sudo ss -tulpn
查看服务器ip
$ ip a
浏览器打开网址,将能看到nginx默认主页:
http://服务器ip
检查nginx日志文件
$ sudo cat /var/log/nginx/onitroad.tech_access.log $ sudo grep 'GET /foo' /var/log/nginx/onitroad.tech_access.log $ sudo tail -f /var/log/nginx/onitroad.tech_error.log
设置防火墙
使用firewall-cmd打开端口80和443
$ sudo firewall-cmd --permanent --zone=public --add-service=http --add-service=https $ sudo firewall-cmd --reload $ sudo firewall-cmd --list-services --zone=public
搜索Nginx软件包
使用yum或者dnf(centos8)搜索软件包
$ sudo yum search nginx $ sudo yum list nginx
查看要安装的Nginx版本信息:
$ sudo yum info nginx
列出所有Nginx模块以及它们的配置文件:
$ sudo yum module list nginx
默认版本为1.14,但我们也可以安装版本1.16。
Last metadata expiration check: 0:18:57 ago on Mon Jun 8 10:07:04 2020. CentOS-8 - AppStream Name Stream Profiles Summary nginx 1.14 [d] common [d] nginx webserver nginx 1.16 common nginx webserver Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled
要安装Nginx版本1.16,执行:
$ sudo yum module reset nginx $ sudo yum module enable nginx:1.16 ## 检测,1.16是否变成了default $ sudo yum module list nginx
在CentOS 8上安装Nginx
安装最新的稳定的nginx服务器(我们上面设置的default项):
$ sudo yum install nginx
如何在CentOS Enterprise Linux 8服务器上安装,设置和配置Nginx服务器?
如何在CentOS 8上安装Nginx并配置静态网站?
如何使用Nginx搭建静态网站?
如何在CentOS 8 Linux服务器上安装Nginx?
启用Nginx服务器
使用systemctl命令启用nginx服务,使其在服务器启动时启动:
$ sudo systemctl enable nginx
输出示例:
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service ? /usr/lib/systemd/system/nginx.service.
启动nginx服务
$ sudo systemctl start nginx
如何启动/停止/重启Nginx服务器
$ sudo systemctl start nginx ## <-- centos8启动nginx服务器 $ sudo systemctl stop nginx ## <-- centos8停止nginx服务器 $ sudo systemctl restart nginx ## <-- centos8重启nginx服务器 $ sudo systemctl reload nginx ## <-- centos8重新加载nginx服务器 $ sudo systemctl status nginx ## <-- centos8查看nginx服务器的状态
创建虚拟域/主机
使用useradd命令为虚拟主机创建一个新用户:
$ sudo useradd -d /home/onitroad.tech -m -k /dev/null -s /usr/sbin/nologin blog-www
其中
-d /home/onitroad.tech
:主目录,用于存储名为onitroad.tech的虚拟域的所有文件-m
:建立一个新目录来存储文件。-k/dev/null
:不要在目录中创建任何点文件。-s/usr/sbin/nologin
:不允许blog-www用户登录服务器。blog-www
:我的虚拟域的用户名onitroad.tech
锁定Linux用户帐户:
$ sudo passwd -l blog-www
使用mkdir命令创建html主目录:
$ sudo mkdir -pv /home/onitroad.tech/html
创建一个主页用于测试
$ sudo vim /home/onitroad.tech/html/index.html
<html> <body> <h2>hello www.onitroad.tech</h2> <p>虚拟主机测试页面</p> </body> </html>
使用chown命令设置权限,
并使用chcon命令更正SELinux权限:
$ sudo chmod -R 0555 /home/onitroad.tech/ $ sudo chown -R blog-www:blog-www /home/onitroad.tech/ $ sudo chcon -vR system_u:object_r:httpd_sys_content_t:s0 /home/onitroad.tech/
创建虚拟域/主机文件:
$ sudo vim /etc/nginx/conf.d/onitroad.tech.conf
内容参考:
server { ## Listen to TCP port 80 ## listen 80; listen [::]:80; ## Set document web root to directory ## root /home/onitroad.tech/html; index index.html; ## Set virtual domain/host name here ## server_name onitroad.tech www.onitroad.tech; ## Set default access and error log file ## access_log /var/log/nginx/onitroad.tech_access.log main; error_log /var/log/nginx/onitroad.tech_error.log; ## Set default error ## location / { try_files $uri $uri/ =404; } }
检查Nginx的语法是否错误:
$ sudo nginx -t
一切ok,则重新加载Nginx服务器,使设置生效:
$ sudo nginx -s reload ## 或者 $ sudo systemcl reload nginx
浏览器打开网站
http://www.onitroad.tech
一切正常,将能看到index.html的内容。
配置Nginx服务器
默认配置信息:
- 配置目录:
/etc/nginx /
- Maibn全局配置文件:
/etc/nginx/nginx.conf
- Nginx 打开的TCP端口:
80
(HTTP),443
(HTTPS) - 默认的文档根目录
/usr/share/nginx/html
- 访问日志文件
/var/log/nginx/access.log
- 错误日志文件
/var/log/nginx/error.log
编辑配置文件:
$ sudo vi /etc/nginx/nginx.conf
输出示例:
# For more information on configuration, see: user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; # Load dynamic modules. See /usr/share/doc/nginx/README.dynamic. include /usr/share/nginx/modules/*.conf; events { worker_connections 1024; } http { log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; include /etc/nginx/mime.types; default_type application/octet-stream; # Load modular configuration files from the /etc/nginx/conf.d directory. # See http://nginx.org/en/docs/ngx_core_module.html#include # for more information. include /etc/nginx/conf.d/*.conf; server { listen 80 default_server; listen [::]:80 default_server; server_name _; root /usr/share/nginx/html; # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; location / { } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } } }
将html/css/js和图像上传到/usr/share/nginx/html /
$ cd /usr/share/nginx/html/ $ sudo cp /backups/onitroad.local/*.html . $ sudo cp /backups/onitroad.local/*.css . $ sudo cp /backups/onitroad.local/*.js .
静态网站就搭建好了。
更新系统
更新内核和更新所有已安装程序包
$ sudo yum updateinfo $ sudo yum update ## 如果更新后需要重启,则重启 $ sudo reboot
日期:2020-03-23 08:03:50 来源:oir作者:oir