一、Nginx的安装
##安装网络下载工具
[root@admin apps]# yum -y install wget
##gcc是linux下的编译器,可以编译 C,C++,Ada,Object C和Java等语言
##pcre是一个perl库,包括perl兼容的正则表达式库,nginx的http模块使用pcre来解析正则表达式,所以需要安装pcre库。
##zlib库提供了很多种压缩和解压缩方式nginx使用zlib对http包的内容进行gzip,所以需要安装
##openssl是web安全通信的基石,没有openssl,可以说我们的信息都是在裸奔
[root@admin apps]# yum -y install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel
##下载安装包
[root@admin apps]# wget http://nginx.org/download/nginx-1.18.0.tar.gz
[root@admin apps]# ls
nginx-1.18.0.tar.gz
##解压
[root@admin apps]# tar -zxvf nginx-1.18.0.tar.gz
[root@admin apps]# cd nginx-1.18.0
##执行默认配置
[root@admin nginx-1.18.0]# ./configure
##开始编译安装
[root@admin nginx-1.18.0]# make
[root@admin nginx-1.18.0]# make install
Configuration summary
+ using system PCRE library
+ OpenSSL library is not used
+ using system zlib library
nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx modules path: "/usr/local/nginx/modules"
nginx configuration prefix: "/usr/local/nginx/conf"
nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
##创建开机自启文件
[root@admin nginx-1.18.0]# vim /lib/systemd/system/nginx.service
##添加以下内容
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
##设置开机自启
[root@admin nginx-1.18.0]# systemctl enable nginx.service
##启动服务
[root@admin sbin]# systemctl start nginx
[root@admin nginx-1.18.0]# systemctl status nginx
● nginx.service - nginx
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since 四 2020-10-20 22:45:52 CST; 6s ago
Process: 12113 ExecStart=/usr/local/nginx/sbin/nginx (code=exited, status=0/SUCCESS)
Main PID: 12114 (nginx)
CGroup: /system.slice/nginx.service
├─12114 nginx: master process /usr/local/nginx/sbin/nginx
└─12115 nginx: worker process
10月 22 22:45:52 admin systemd[1]: Starting nginx...
10月 22 22:45:52 admin systemd[1]: Started nginx.
二、Nginx的配置
2.1 HTTP服务
2.1.1 创建静态网页
在
/opt/apps/
目录创建一个www.my.com
目录,然后添加一个index.html
文件,并在该文件中添加一些内容。
[root@admin apps]# mkdir www.my.com
[root@admin apps]# ls
nginx-1.18.0 www.my.com
[root@admin apps]# cd www.my.com/
[root@admin www.my.com]# echo "哈哈哈" >> index.html
[root@admin www.my.com]# echo "111" >> index.html
[root@admin www.my.com]# ls
index.html
2.1.2 修改Nginx配置
[root@admin www.my.com]# vim /usr/local/nginx/conf/nginx.conf
##修改以下内容
server {
listen 80;
server_name www.my.com;
location / {
root /opt/apps/www.my.com;
index index.html index.htm;
}
}
include /usr/local/nginx/conf/vhost/*.conf;