苹果系统安装nginx

苹果系统安装nginx

引言

换电脑或者环境的时候需要重新安装并配置nginx环境,所以写了个脚本来处理繁琐的配置等工作;这个脚本能够实现复制nginx.conf,并配置端口号和用户目录;日志在 /var/log/nginx/

准备工作

创建文件夹

~/Documents/ConfigFile/nginx

准备deploy_mac_nginx.sh, nginx.conf

准备deploy_mac_nginx.sh
echo -n "choose mode: 0:reinstall nginx 1:copy conf file"
read mode

if [ $mode -eq 0 ];then
    # 卸载nginx
    echo "cleaning nginx..."
    brew uninstall nginx

    echo -n "not delete log files? yes:No"
    read delete_log_files

    if [ $delete_log_files == "yes" ];then
        echo "deleting nginx log files..."
        # 删除旧目录
        sudo rm -rf /var/log/nginx
    fi

    # 安装nginx 并查看信息
    echo "install nginx..."
    brew install nginx
    brew info nginx

    if [ $delete_log_files == "yes" ];then
        echo "create log dir..."

        # 创建日志目录
        sudo mkdir /var/log/nginx

        # 创建权限
        sudo chmod -R 777 /var/log/nginx
    fi
fi

# # 复制文件
echo "copy nginx.conf..."
cp ~/Documents/ConfigFile/nginx/nginx_local.conf /usr/local/etc/nginx/nginx.conf

# 进入config文件夹
cd /usr/local/etc/nginx/

# 配置端口号
echo -n "请输入端口号(默认80):"
read port

if [ $port -gt 0 ];then
    for file in nginx.conf; do sed -i '' 's/type_port_here/'$port'/g' $file; done
else
    for file in nginx.conf; do sed -i '' 's/type_port_here/80/g' $file; done
fi

# 配置用户名称
echo -n "请输入用户名(默认mac):"
read username

if [ -n "$username" ] ;then
    for file in nginx.conf; do sed -i '' 's/type_user_name_here/'$username'/g' $file; done
else
    for file in nginx.conf; do sed -i '' 's/type_user_name_here/mac/g' $file; done
fi

启动nginx
echo "starting nginx..."
brew services restart nginx
nginx.conf

# user  nginx;
worker_processes  1;

# pid /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log /var/log/nginx/server_access.log;
	error_log /var/log/nginx/server_error.log;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;
    gzip  on;

    server {
        listen type_port_here;
        server_name localhost;

        location / {
            root /Users/type_user_name_here/Desktop/nginx_html;
            index index.php index.html index.htm;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        }

        location /blog {
            try_files $uri $uri/ /blog/php/index.php?$query_string;
        }

        location = /favicon.ico { access_log off; log_not_found off; }
        location = /robots.txt  { access_log off; log_not_found off; }

        location ~ \.php$ {

            root /Users/type_user_name_here/Desktop/nginx_html;
            # 设置监听端口
            fastcgi_pass   127.0.0.1:9000;
            # 设置nginx的默认首页文件(上面已经设置过了,可以删除)
            fastcgi_index  index.php;
            # 设置脚本文件请求的路径
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            # 引入fastcgi的配置文件
            include        fastcgi_params;
        }

        location ~ /\.(?!well-known).* {
            deny all;
        }
    }
    
    include servers/*;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值