2. 安装
[iyunv@server1 mnt]# yum install pcre-devel openssl-devel -y
[iyunv@server1 mnt]# cd nginx-1.9.14/
[iyunv@server1 nginx-1.9.14]# ./configure \
> --prefix=/opt/lnmp/nginx \
> --with-http_ssl_module \
> --with-http_sub_module \
> --with-http_stub_status_module
[iyunv@server1 nginx-1.9.14]# make ##编译
[iyunv@server1 nginx-1.9.14]# make install ##安装
[iyunv@server1 nginx-1.9.14]# ls /opt/lnmp/nginx/ #安装完成查看
conf html logs sbin
3.配置
[iyunv@server1 nginx-1.9.14]# cd /opt/lnmp/nginx/
[iyunv@server1 nginx]# vim conf/nginx.conf
2 user nginx nginx;##用户和组,可以只写用户
3 worker_processes 2;##cpu个数,不能超过lscpu显示cpu个数
12 events {
13 use epoll; ##nginx epoll 采用异步非阻塞模式 apache --select 同步阻塞机制 io复用模型类型
14 worker_connections 4096;##连接数
15 }
[iyunv@server1 nginx]# vim /etc/profile
export PATH=$PATH:/opt/lnmp/nginx/sbin #添加nginx执行路径
[iyunv@server1 nginx]# source /etc/profile
[iyunv@server1 nginx]# nginx -t #检查nginx有无错误
nginx: the configuration file /opt/lnmp/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /opt/lnmp/nginx/conf/nginx.conf test is successful
[iyunv@server1 nginx]# nginx #启动nginx
nginx -s reload #重起
ngnix -s stop #关闭
测试:
[iyunv@server1 nginx]# curl -I localhost ##检测http协议提供程序
HTTP/1.1 200 OK
Server: willis/
Date: Sun, 11 Sep 2016 01:26:39 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Sun, 11 Sep 2016 01:19:44 GMT
Connection: keep-alive
ETag: "57d4b130-264"
Accept-Ranges: bytes
[iyunv@server1 nginx]# curl localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</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>
[iyunv@server1 nginx]# cd html/ #默认发布目录
[iyunv@server1 html]# ls
50x.html index.html
网页测试:
4.添加https
[iyunv@server1 nginx]# pwd
/opt/lnmp/nginx
[iyunv@server1 nginx]# vim conf/nginx.conf
# HTTPS server #开启HTTPS功能
server {
listen 443 ssl;
server_name localhost;
ssl_certificate cert.pem;
ssl_certificate_key cert.pem; #修改证书名
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
root html;
index index.html index.htm;
}
}
}
[iyunv@server1 nginx]# cd /etc/pki/tls/certs/
[iyunv@server1 certs]# make cert.pem #新建证书
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:shaanxi
Locality Name (eg, city) [Default City]:xi'an
Organization Name (eg, company) [Default Company Ltd]:redhat
Organizational Unit Name (eg, section) []:Linux
Common Name (eg, your name or your server's hostname) []:localhost
Email Address []:upsun_sun@163.com
[iyunv@server1 certs]# cp cert.pem /opt/lnmp/nginx/conf/
[iyunv@server1 certs]# nginx -t
nginx: the configuration file /opt/lnmp/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /opt/lnmp/nginx/conf/nginx.conf test is successful
[iyunv@server1 certs]# nginx -s reload
测试:
5.虚拟主机
[iyunv@server1 nginx]# pwd
/opt/lnmp/nginx
[iyunv@server1 nginx]# vim conf/nginx.conf
在http{}中添加
server {
listen 80;
server_name www.willis.com;
location / {
root /virtual/willis/html;
index index.html;
}
}
server {
listen 80;
server_name www.linux.com;
location / {
root /virtual/linux/html;
index index.html;
}
}
[iyunv@server1 nginx]# mkdir -p /virtual/willis/html
[iyunv@server1 nginx]# mkdir -p /virtual/linux/html
[iyunv@server1 nginx]# echo www.willis.com>/virtual/willis/html/index.html
[iyunv@server1 nginx]# echo www.linux.com>/virtual/linux/html/index.html
[iyunv@server1 nginx]# vim /etc/hosts
172.25.254.1 www.willis.com
172.25.254.1 www.linux.com
[iyunv@server1 nginx]# nginx -t
nginx: the configuration file /opt/lnmp/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /opt/lnmp/nginx/conf/nginx.conf test is successful
[iyunv@server1 nginx]# nginx -s reload
测试: