实验环境:
vmware、centos7(最小化安装)
硬件配置:
1核2G,30G磁盘,网络桥接方式(dhcp分配ip为192.168.1.18)
操作系统配置:
系统安装的时候可以先配置hostname,否则需要进入系统通过hostnamectl命令永久修改hostname,试验机命名未remote2。最好通过该命令修改,网络所说的修改hosts或network文件都不是正规修改方式,但能实现需求。
系统安装完成后磁盘自动分配如下:
[root@remote2 ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
devtmpfs 898M 0 898M 0% /dev
tmpfs 910M 0 910M 0% /dev/shm
tmpfs 910M 9.5M 901M 2% /run
tmpfs 910M 0 910M 0% /sys/fs/cgroup
/dev/mapper/centos-root 27G 1.3G 26G 5% /
/dev/sda1 1014M 150M 865M 15% /boot
tmpfs 182M 0 182M 0% /run/user/0
为免引起不必要的问题,关闭selinux后再进行其他配置,关闭命令如下:
setenforce 0
为方便编辑文件,有更好的文本编辑体验,安装vim,基于YUM的安装命令如下:
yum install vim -y
为免并发上升导致socket数量到达瓶颈或程序打开文件数到达瓶颈影响服务,增加打开文件数,编辑 /etc/security/limits.conf 文件,增加以下内容:
* soft nproc 65535
* hard nproc 65535
* soft nofile 65535
* hard nofile 65535
规划:
- web容器选用nginx,服务端口为80
- 页面存放路径:/data/html
- 日志存放路径:/var/log/nginx
软件安装&配置:
- 安装nginx:
增加nginx yum安装的配置文件
vim /etc/yum.repos.d/nginx.repo
增加内容如下(从nginx官网说明复制):
[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
然后执行安装命令,本次安装的版本是1.18.0:
yum install nginx -y
安装完成后,
会自动生成nginx用户
[root@remote2 ~]# id nginx
uid=997(nginx) gid=995(nginx) 组=995(nginx)
/etc目录下会生成nginx目录,里面存放着nginx的配置文件
检查以下目录是否存在,不存在则创建:
页面存放路径:/data/html
日志存放路径:/var/log/nginx
- 配置nginx:
修改全局配置部分
vim /etc/nginx/nginx.conf
nginx.conf是安装完成后默认生成的配置文件,找到/路径的节点,将配置修改为:
user root;
worker_processes 2;
将运行nginx的用户改为root(原为nginx),为免前后端分离的环境读取文件异常,或其它由非nginx用户生成的文件导致nginx读取文件失败。
将进程修改为2,也可设置成auto,建议值是cpu个数乘以2。这个数值将影响nginx生成worker进程的进程数。
检查error_log存放路径是否与规划一致
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
这里与规划一致,不做修改。
这里pid配置为记录nginx进程id的文件,修改后可能导致Systemd管理nginx异常,其修改需要配合相应的unit 配置文件
[root@remote2 system]# vim nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
[Install]
WantedBy=multi-user.target
这里的PIDFile需要与nginx配置文件的PID保持一致。
修改http配置部分
检查log路径是否跟规划一致
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;
默认即为规划的路径,不做修改。
log_format配置为日志所需记录的内容,可按需增加
修改server配置部分
vim /etc/nginx/conf.d/default.conf
default.conf是安装完成后默认生成的配置文件,找到/路径的节点,将配置修改为:
location / {
root /data/html;
index index.html index.htm;
}
root配置指客户端访问/ (根路径)下的文件时,Nginx去/data/html目录下找对应的文件;index配置指url不指定文件时,Nginx将默认返回index.html文件,次之返回index.htm文件。
启动nginx
systemctl start nginx
防火墙配置,开放80端口,这里使用firewalld管理,centos7以下的版本使用iptables进行管理,如果centos7使用iptables进行管理需要另外安装该工具。
[root@remote2 ~]# firewall-cmd --zone=public --add-port=80/tcp
success
[root@remote2 ~]# firewall-cmd --zone=public --add-port=80/tcp --permanent
success
在html目录新建一个页面测试nginx服务是否正常
[root@remote2 ~]# cd /data/html
[root@remote2 html]# vim hello.html
随便输入测试内容到hello.html文件。
用浏览器打开http://192.168.1.18/hello.html
检查响应与hello.html文件一致。
到日志目录/var/log/nginx/检查日志是否正常输出。
此毕,后续文章再去写nginx配置优化的内容。