苹果系统安装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/*;
}