系统环境:CentOS Linux release 7.9.2009 (Core)
本文适用于 centos、redhat(linux发行版)
1. nginx 源码安装脚本
#!/usr/bin/env bash
###################################################################################
# 控制台颜色
BLACK="\033[1;30m"
RED="\033[1;31m"
GREEN="\033[1;32m"
YELLOW="\033[1;33m"
BLUE="\033[1;34m"
PURPLE="\033[1;35m"
CYAN="\033[1;36m"
RESET="$(tput sgr0)"
###################################################################################
printf "${BLUE}\n"
cat << EOF
###################################################################################
# 采用编译方式安装 Nginx, 并将其注册为 systemd 服务
# 默认下载安装 1.20.1 版本,安装路径为:/usr/local/nginx
# @system: 适用于 CentOS7+
# @author: Zhang Peng
###################################################################################
EOF
printf "${RESET}\n"
command -v yum > /dev/null 2>&1 || {
printf "${RED}Require yum but it's not installed.${RESET}\n";
exit 1;
}
printf "\n${GREEN}>>>>>>>> install nginx begin${RESET}\n"
if [[ $# -lt 1 ]] || [[ $# -lt 2 ]]; then
printf "${PURPLE}[Hint]\n"
printf "\t Usage: sh nginx-install.sh [version] \n"
printf "\t Default: sh nginx-install.sh 1.20.1\n"
printf "\t Example: sh nginx-install.sh 1.20.1 \n"
printf "${RESET}\n"
fi
temp=/opt/nginx
version=1.20.1
if [[ -n $1 ]]; then
version=$1
fi
# install info
printf "${PURPLE}[Install Info]\n"
printf "\t version = ${version}\n"
printf "${RESET}\n"
printf "${CYAN}>>>> install required libs${RESET}\n"
yum -y install pcre pcre-devel openssl openssl-devel zlib zlib-devel gcc-c++ make wget &> /dev/null; if [ $? -eq 0 ];then echo "install completed"; else echo "install fail" ;fi
# download and decompression
printf "${CYAN}>>>> download nginx${RESET}\n"
mkdir -p ${temp}
wget -O ${temp}/nginx-${version}.tar.gz http://nginx.org/download/nginx-${version}.tar.gz
tar zxf ${temp}/nginx-${version}.tar.gz -C ${temp}
# configure and makefile
printf "${CYAN}>>>> compile nginx${RESET}\n"
cd ${temp}/nginx-${version}
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre
make && make install &> /dev/null; if [ $? -eq 0 ];then echo "compile completed"; else echo "compile fail" ;fi
rm -rf ${temp}
cd -
# setting systemd service
printf "${CYAN}>>>> set nginx as a systemd service${RESET}\n"
wget -N https://gitee.com/turnon/linux-tutorial/raw/master/codes/linux/soft/config/nginx/nginx.service -O /usr/lib/systemd/system/nginx.service
chmod +x /usr/lib/systemd/system/nginx.service
# boot nginx
printf "${CYAN}>>>> start nginx${RESET}\n"
systemctl enable nginx.service
systemctl start nginx.service
printf "\n${GREEN}<<<<<<<< install nginx end${RESET}\n"
printf "\n${PURPLE}nginx service status: ${RESET}\n"
systemctl status nginx
2 更改nginx配置文件
按照第一步脚本安装nginx后,默认nginx配置文件在:/usr/local/nginx/conf/nginx.conf
修改 http 下的 server 内容
源内容
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
修改后内容
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /opt/packages; # 指定访问主机目录
autoindex on; # 开启目录浏览功能
autoindex_exact_size on; # 开启详细文件大小统计,让文件大小显示MB,GB单位,默认为b
autoindex_localtime on; # 开启以服务器本地时区显示文件修改日期!
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
3.效果
nginx常用命令
nginx –s stop 停止nginx的服务
nginx –s reload 不停止nginx的服务,重新加载配置文件。
nginx –t 检测配置文件是否有错误。
nginx -s quit 关闭nginx,完整有序的停止nginx,保存相关信息
nginx -V 除版本信息外还显示配置参数信息(大写字母V)
nginx -s reopen 重新打开日志文件