12.6 Nginx安装
安装过程
# cd /usr/local/src
# wget http://nginx.org/download/nginx-1.14.0.tar.gz (一般来说,偶数版本为稳定版,适合生产环境)
# tar xzf nginx-1.14.0.tar.gz
# cd nginx-1.14.0/
# ./configure --prefix=/usr/local/nginx
# make && make install
设置过程
# cd /usr/local/nginx/conf/
# mv nginx.conf nginx.conf1 //把系统自带的配置文件改名备份
# vim nginx.conf //创建新的配置文件,写入以下内容,参考链接
#/etc/init.d/nginx start
# ps aux |grep nginx
# ps aux |grep php-fpm
测试
# curl localhost
#vim /usr/local/nginx/html/1.php //创建一个php文件,简单写入以下内容
# curl localhost/1.php //可以正常解析php
12.7 Nginx默认虚拟主机
# vim /usr/local/nginx/conf/nginx.conf //编辑配置文件,删除最后几行并添加一行内容
# mkdir /usr/local/nginx/conf/vhost
#cd /usr/local/nginx/conf/vhost
# vim aaa.com.conf //新建并添加如下内容
server
{
listen 80 default_server; //有这个标记就是默认虚拟主机
server_name aaa.com;
index index.html index.htm index.php;
root /data/wwwroot/default;
}
# mkdir -p /data/wwwroot/default
# echo “This is a default site.”>/data/wwwroot/default/index.html
# /usr/local/nginx/sbin/nginx -t //检查语法
# /usr/local/nginx/sbin/nginx -s reload //重新加载
测试
# curl localhost
下面说一下总结默认虚拟主机的两种方法
1.在/usr/local/nginx/conf/vhost目录下排在第一位,比如下面aaa.com.conf就是默认的虚拟主机
2.改配置文件
12.8 Nginx用户认证
#vim /usr/local/nginx/conf/vhost/test.com.conf //写入如下内容
server
{
listen 80;
server_name test.com;
index index.html index.htm index.php;
root /data/wwwroot/test.com;
location /
{
auth_basic "Auth";
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
}
}
# yum install -y httpd