LNMP架构(二)之nginx安装,虚拟主机,用户认证,域名重定向

Nginx安装javascript

cd /usr/local/src
wget http://nginx.org/download/nginx-1.12.1.tar.gz
tar zxf nginx-1.12.1.tar.gz
cd nginx-1.12.1
./configure --prefix=/usr/local/nginx
make &&  make install
vim /etc/init.d/nginx //复制以下内容(参考https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/etc_init.d_nginx )

输入图片说明

#!/bin/bash
# chkconfig: - 30 21
# description: http service.
# Source Function Library
. /etc/init.d/functions
# Nginx Settings
NGINX_SBIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/logs/nginx.pid"
RETVAL=0
prog="Nginx"
start()
{
echo -n $"Starting $prog: "
mkdir -p /dev/shm/nginx_temp
daemon $NGINX_SBIN -c $NGINX_CONF
RETVAL=$?
echo
return $RETVAL
}
stop()
{
echo -n $"Stopping $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -TERM
rm -rf /dev/shm/nginx_temp
RETVAL=$?
echo
return $RETVAL
}
reload()
{
echo -n $"Reloading $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -HUP
RETVAL=$?
echo
return $RETVAL
}
restart()
{
stop
start
}
configtest()
{
$NGINX_SBIN -c $NGINX_CONF -t
return 0
}
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
restart
;;
configtest)
configtest
;;
*)
echo $"Usage: $0 {start|stop|reload|restart|configtest}"
RETVAL=1
esac
exit $RETVAL
chmod 755 /etc/init.d/nginx
chkconfig --add nginx
chkconfig nginx on
修改文件权限为755,并设置开启启动。

输入图片说明

cd /usr/local/nginx/conf/; mv nginx.conf nginx.conf.bak
切换到conf目录,备份原配置文件,新建一个配置文件,内容以下。
vim nginx.conf //写入以下内容(参考https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/nginx.conf)
user nobody nobody;                    //user用来定义启动nginx是哪一个用户
worker_processes 2;                      //定义nginx的子进程
error_log /usr/local/nginx/logs/nginx_error.log crit;        //错误日志
pid /usr/local/nginx/logs/nginx.pid;                                
worker_rlimit_nofile 51200;                    //nginx最多能打开多少个文件
events
{
use epoll;
worker_connections 6000;                    //nginx最大的链接数
}
http
{
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 3526;
server_names_hash_max_size 4096;
log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
' $host "$request_uri" $status'
' "$http_referer" "$http_user_agent"';
sendfile on;
tcp_nopush on;
keepalive_timeout 30;
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
connection_pool_size 256;
client_header_buffer_size 1k;
large_client_header_buffers 8 4k;
request_pool_size 4k;
output_buffers 4 32k;
postpone_output 1460;
client_max_body_size 10m;
client_body_buffer_size 256k;
client_body_temp_path /usr/local/nginx/client_body_temp;
proxy_temp_path /usr/local/nginx/proxy_temp;
fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
fastcgi_intercept_errors on;
tcp_nodelay on;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 8k;
gzip_comp_level 5;
gzip_http_version 1.1;
gzip_types text/plain application/x-javascript text/css text/htm
application/xml;
server
{
listen 80;
server_name localhost;
index index.html index.htm index.php;
root /usr/local/nginx/html;            //定义网址根目录,默认虚拟主机
location ~ \.php$                    //定义支持解析php
{
include fastcgi_params;
fastcgi_pass unix:/tmp/php-fcgi.sock;  
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
}
}
}
/usr/local/nginx/sbin/nginx -t            //检查配置文件语法是否正确
/etc/init.d/nginx  start                       //开启nginx服务
netstat -lntp |grep 80

输入图片说明

测试解析php
vi /usr/local/nginx/html/1.php //加入以下内容
<?php
    echo "test php scripts.";
?>
curl localhost/1.php

输入图片说明

Nginx默认虚拟机php

vim /usr/local/nginx/conf/nginx.conf //进入配置文件增长以下内容,去掉server。
include vhost/*.conf;            //定义包含vhost目录下的配置文件,该vhost在conf目录下。
/usr/local/nginx/sbin/nginx -t               //检查语法
mkdir /usr/local/nginx/conf/vhost            //建立vhost目录
cd !$;  

vim default.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        //从新加载nginx服务
curl localhost
curl -x127.0.0.1:80 123.com

输入图片说明

Nginx用户认证css

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  /                    //定义用户认证的目录斜杠表示当前站点,要设置站点下的目录直接/dir/便可,若是要设置单个页面认证的话,用~ admin.php 便可。
    {
        auth_basic              "Auth";
        auth_basic_user_file   /usr/local/nginx/conf/htpasswd;
}
}
yum install -y httpd                //安装httpd以支持htpasswd命令
htpasswd -cm /usr/local/nginx/conf/htpasswd aming    //建立aming的密码文件,-m用md5加密,建立第二个就不须要-c参数了。
/usr/local/nginx/sbin/nginx -t               //检查语法
/usr/local/nginx/sbin/nginx -s reload        //从新加载nginx服务
mkdir /data/wwwroot/test.com
vi /data/wwwroot/test.com/index.html

输入图片说明

Nginx域名重定向html

更改test.com.conf
server
{
    listen 80;
    server_name test.com  test1.com  test2.com;
    index index.html index.htm index.php;
    root /data/wwwroot/test.com;
    if ($host != 'test.com' ) {
        rewrite  ^/(.*)$  http://www.baidu.com/$1  permanent;
    }
}
server_name后面支持写多个域名,这里要和httpd的作一个对比

permanent为永久重定向,状态码为301,若是写redirect则为302,建议设置永久重定向。

输入图片说明

相关文章
相关标签/搜索