安装并启动Nginx
1 - Nginx简介
Nginx (engine x) 是一个高性能的轻量级的Web和反向代理服务器,同时也提供了IMAP/POP3/SMTP服务。
Nginx源代码以类BSD许可证的形式发布,特点是稳定性好、功能丰富、示例配置文件完整、占用资源少、并发能力强。
官网信息
- HomePage: http://nginx.org/
- Documentation: http://nginx.org/en/docs/
- Download: http://nginx.org/en/download.html/
文件与目录
- 配置文件:
/etc/nginx/nginx.conf
- 日志文件目录:
/var/log/nginx/
- nginx初始页面:
/usr/share/nginx/
目录下的index.html文件
2 - 安装Nginx
官网步骤:http://nginx.org/en/linux_packages.html#RHEL-CentOS
[Anliven@h202 ~]$ sudo vim /etc/yum.repos.d/nginx.repo
[Anliven@h202 ~]$
[Anliven@h202 ~]$ cat /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[Anliven@h202 ~]$
[Anliven@h202 ~]$ sudo yum -y install nginx
Loaded plugins: fastestmirror, langpacks
Determining fastest mirrors
* base: mirrors.aliyun.com
* extras: mirrors.163.com
* updates: mirror.bit.edu.cn
......
......
......
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!
[Anliven@h202 ~]$
3 - 配置Nginx
3.1 修改默认配置
配置文件: /etc/nginx/conf.d/default.conf
[Anliven@h202 ~]$ cd /etc/nginx/conf.d/
[Anliven@h202 conf.d]$ pwd
/etc/nginx/conf.d
[Anliven@h202 conf.d]$ ll
total 4
-rw-r--r-- 1 root root 1093 Aug 13 23:02 default.conf
[Anliven@h202 conf.d]$
[Anliven@h202 conf.d]$ sudo vim default.conf
[Anliven@h202 conf.d]$ cat default.conf |grep -v "#" |grep -Ev "^$"
server {
listen 80;
server_name 192.168.16.202;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
location = /favicon.ico {
log_not_found off;
access_log off;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
[Anliven@h202 conf.d]$
3.2 修改Nginx配置文件
/etc/nginx/nginx.conf
中user参数,修改为root
[Anliven@h202 ~]$ sudo vim /etc/nginx/nginx.conf
[Anliven@h202 ~]$
[Anliven@h202 ~]$ cat /etc/nginx/nginx.conf |grep -v "#" |grep -Ev "^$"
user root;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
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;
keepalive_timeout 65;
include /etc/nginx/conf.d/*.conf;
}
[Anliven@h202 ~]$
3.3 确认防火墙状态并重启服务
[Anliven@h202 ~]$ sudo firewall-cmd --zone=public --permanent --add-port=80/tcp
success
[Anliven@h202 ~]$ sudo firewall-cmd --reload
success
[Anliven@h202 ~]$ sudo firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: enp0s3 enp0s8
sources:
services: ssh dhcpv6-client
ports: 80/tcp
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
[Anliven@h202 ~]$
[Anliven@h202 ~]$ cat /etc/selinux/config |grep "SELINUX=" |grep -v "#"
SELINUX=disabled
[Anliven@h202 ~]$
[Anliven@h202 ~]$ sudo service nginx restart
Redirecting to /bin/systemctl restart nginx.service
[Anliven@h202 ~]$
4 - 访问页面
在/usr/share/nginx/
目录下的index.html文件,就是关于nginx介绍的页面
4.1 访问Nginx介绍页面
4.2 修改Nginx介绍页面
[Anliven@h202 html]$ pwd
/usr/share/nginx/html
[Anliven@h202 html]$
[Anliven@h202 html]$ sudo vim index.html
[Anliven@h202 html]$ cat index.html
<!DOCTYPE html>
<html>
<head>
<title>This is a test!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to MyNginx!</h1>
<p>Action is the antidote to despair!</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[Anliven@h202 html]$
刷新浏览器页面
5 - 参考信息
- The Complete NGINX Cookbook:https://www.nginx.com/resources/library/complete-nginx-cookbook/
- Nginx配置详解:https://www.runoob.com/w3cnote/nginx-setup-intro.html
- Nginx反向代理与负载均衡详解:https://www.runoob.com/w3cnote/nginx-proxy-balancing.html
- Nginx 容器教程:http://www.ruanyifeng.com/blog/2018/02/nginx-docker.html
行动是绝望的解药!
欢迎转载和引用,但请在明显处保留原文链接和原作者信息!
本博客内容多为个人工作与学习的记录,少数内容来自于网络并略有修改,已尽力标明原文链接和转载说明。如有冒犯,即刻删除!
以所舍,求所得,有所获,方所成。