yum安装法法简单,但是缺少灵活性;无法自定义模块及安装路径
yum源配置:http://nginx.org/en/linux_packages.html
创建一个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
[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
yum makecache
yum -y install nginx
这样yum就能装好,关闭防火墙后可在浏览器访问出现欢迎页面
----------------------------------------l
2.2 源码安装
源码包获取:http://nginx.org/download/
源码安装法法过程繁琐,但是可定制化安装
源码安装的环境说明:
系统:CentOS 7.4
软件:nginx-1.14.0.tar.gz
其他所需软件:openssl-1.0.2d.tar.gz、pcre-8.37.tar.gz、zlib-1.2.8.tar.gz
安装方式:源码编译安装
安装位置:/opt/data/nginx
下载地址:http://nginx.org/download
1.安装环境准备,安装依赖包,创建安装目录
[root@localhost ~]# mkdir -p /opt/data/nginx
[root@localhost ~]# groupadd nginx
[root@localhost ~]# useradd -g nginx nginx
[root@localhost ~]# yum install gcc gcc-c++ make recp pcre-devel openssl openssl-devel -y
[root@localhost ~]# tar -xf nginx-1.14.0.tar.gz -C /usr/local/src/
2.编译安装
[root@localhost nginx-1.14.0]# ./configure \
> --prefix=/opt/data/nginx \
> --with-http_stub_status_module \
> --with-http_ssl_module \
> --with-stream
[root@localhost nginx-1.14.0]# make &make install
配置环境变量
[root@localhost sbin]# ./nginx -t
[root@localhost sbin]# vim /etc/profile
export PATH=/opt/data/nginx/sbin:$PATH
[root@localhost sbin]# source /etc/profile
4.Nginx配置
[root@localhost nginx]# ln -s /opt/data/nginx/conf /etc/nginx
[root@localhost nginx]# vim nginx.conf
user nginx;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
access_log off;
client_max_body_size 128M;
include /etc/nginx/conf.d/*.conf;
}
[root@localhost nginx]# mkdir /etc/nginx/conf.d
[root@localhost nginx]# mkdir /var/log/nginx
[root@localhost nginx]# chown -R nginx:nginx /var/log/nginx
5.Nginx启动
vi /etc/nginx/nginx.conf
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/opt/data/nginx/sbin/nginx -t -c /opt/data/nginx/conf/nginx.conf
ExecStart=/opt/data/nginx/sbin/nginx -c /opt/data/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
创建一个用systemctl启动的
[root@localhost html]# vi /lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/opt/data/nginx/sbin/nginx -t -c /opt/data/nginx/conf/nginx.conf
ExecStart=/opt/data/nginx/sbin/nginx -c /opt/data/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
3.Nginx虚拟主机
所谓的虚拟主机,在web服务器里就是一个独立的网络站点,这个站点对应独立的域名(也可能是IP或端口),具有独立的程序及资源目录,可以独立地对外服务供用户访问
1.创建域名对应的站点目录文件
[root@localhost nginx]# mkdir -p /data/xingyun/basic
[root@localhost nginx]# mkdir -p /data/anttech/basic
[root@localhost nginx]# chown -R nginx:nginx /data/xingyun/basic/
[root@localhost nginx]# chown -R nginx:nginx /data/anttech/basic/
[root@localhost conf.d]# mkdir -p /data/xingyun/log
[root@localhost conf.d]# mkdir -p /data/anttech/log
[root@localhost conf.d]# chown -R nginx:nginx /data/xingyun/log/
[root@localhost conf.d]# chown -R nginx:nginx /data/anttech/log/
[root@localhost conf.d]# sed -ri '/^SELINUX/c\SELINUX=disabled' /etc/selinux/config
[root@localhost conf.d]# setenforce 0
[root@localhost conf.d]# firewall-cmd --permanent --add-service=http
[root@localhost conf.d]# firewall-cmd --permanent --add-servcie=https
[root@localhost conf.d]# firewall-cmd --reload
[root@localhost conf.d]# firewall-cmd --permanent --list-all
2.虚拟主机
cd /etc/nginx/conf.d/
[root@localhost conf.d]# vim xingyun.conf
server {
listen 192.168.1.104:80;
server_name www.xingyun.com;
access_log /data/xingyun/log/access.log combined;
location / {
root /data/xingyun/basic;
index index.html index.htm;
}
}
[root@localhost conf.d]# vim anttech.conf
server {
listen 192.168.1.104:80;
server_name www.anttech.com;
access_log /data/anttech/log/access.log combined;
location / {
root /data/anttech/basic;
index index.html index.htm;
}
}
3.域名解析
C:\Windows\System32\drivers\etc
192.168.1.104 www.anttech.com
192.168.1.104 www.xingyun.com
4.结果测试
[root@localhost conf.d]# echo "this is a test from xingyun" >> /data/xingyun/basic/index.html
[root@localhost conf.d]# echo "this is a test from anttech" >> /data/anttech/basic/index.html
接下来可以去浏览器访问了
--------------------下节内容:1.虚拟主机基于端口,IP,域名的访问,
2. 地址重写
QQ:2858226830