▲单独架构的小伙伴看这里:(学习杰哥视频的作业第1-2天)
1、编译安装LNMP,配置自定义404页面,配置访问日志为json格式。
一、编译安装nginx:
1)安装相关的包
#yum install -y gcc pcre-devel openssl-devel zlib-devel
2)创建账号并编译安装
# tar -zxvf nginx-1.16.1.tar.gz
# useradd -r -s /sbin/nologin nginx
# cd nginx-1.16.1/
# ./configure --prefix=/apps/nginx \> --user=nginx \> --group=nginx \> --with-http_ssl_module \> --with-http_v2_module \> --with-http_realip_module \> --with-http_stub_status_module \> --with-http_gzip_static_module \> --with-pcre \> --with-stream \> --with-stream_ssl_module \> --with-stream_realip_module
# make && make install
二、修改主配置文件
# vim /apps/nginx/conf/nginx.conf
http {
#JSON格式日志及变量 accesson_json
log_format access_json '{"@timestamp":"$time_iso8601",'
'"host":"$server_addr",'
'"clientip":"$remote_addr",'
'"size":$body_bytes_sent,'
'"responsetime":$request_time,'
'"upstreamtime":"$upstream_response_time",'
'"upstreamhost":"$upstream_addr",'
'"http_host":"$host",'
'"uri":"$uri",'
'"domain":"$host",'
'"xff":"$http_x_forwarded_for",'
'"referer":"$http_referer",'
'"tcp_xff":"$proxy_protocol_addr",'
'"http_user_agent":"$http_user_agent",'
'"status":"$status"}';
}
三、修改个性化的配置文件
# vim /apps/nginx/conf.d/json_nginx.conf
server {
listen 80;
server_name www.x.com;
root /apps/nginxhtml;
error_page 404 /404.html;
location = /404.html {}
access_log /var/log/nginx/magedu_net_access_json.log access_json
}
2、配置虚拟主机,实现https访问www.x.com(x.com为自己定义的域名)
一、⾃签名CA证书
[root@s2 ~]# cd /apps/nginx/
[root@s2 nginx]# mkdir certs
[root@s2 nginx]# cd certs/
[root@s2 nginx]# openssl req -newkey rsa:4096 -nodes -sha256 -keyout ca.key -x509 -
days 3650 -out ca.crt #⾃签名CA证书
.....
Country Name (2 letter code) [XX]:CN #国家代码,https://country-code.cl/
State or Province Name (full name) []:BeiJing #省份
Locality Name (eg, city) [Default City]:Beijing #城市名称
Organization Name (eg, company) [Default Company Ltd]:magedu.Ltd #公司名称
Organizational Unit Name (eg, section) []:magedu #部⻔
Common Name (eg, your name or your server's hostname) []:magedu.ca #通⽤名称
Email Address []:2973707860@qq.com #邮箱
[root@s2 certs]# ll ca.crt
-rw-r--r-- 1 root root 2118 Feb 22 12:10 ca.crt
二、⾃制key和csr⽂件
[root@s2 certs]# openssl req -newkey rsa:4096 -nodes -sha256 -keyout www.x.com.key -out www.x.com.csr
......
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:BeiJing
Locality Name (eg, city) [Default City]:BeiJing
Organization Name (eg, company) [Default Company Ltd]:magedu.net
Organizational Unit Name (eg, section) []:magedu.net
Common Name (eg, your name or your server's hostname) []:www.magedu.net
Email Address []:2973707860@qq.com
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
三、签发证书
[root@s2 certs]# openssl x509 -req -days 3650 -in www.x.com.csr -CA ca.crt -CAkey
ca.key -CAcreateserial -out www.x.com.crt
Signature ok
subject=/C=CN/ST=BeiJing/L=BeiJing/O=magedu.net/OU=magedu.net/CN=www.magedu.net/emailAd
dress=2973707860@qq.com
Getting CA Private Key
四、实现多域名HTTPS:
[root@s2 certs]# openssl x509 -in www.x.com.crt -noout -text
# vim /apps/nginx/conf.d/ssh.conf
server {
listen 443 ssl;
server_name www.x.com;
root /apps/nginxhtml;
ssl_certificate /apps/nginx/certs/www.x.com.crt;
ssl_certificate_key /apps/nginx/certs/www.x.com.key;
ssl_session_cache shared:sslcache:20m;
ssl_session_timeout 10m;
}
#重启Nginx并访问验证