查看服务器版本号
cat /etc/redhat-release
安装依赖
yum update -y
yum install -y gcc gcc-c++ make libtool zlib zlib-devel openssl openssl-devel pcre pcre-devel
初始化
adduser www
groupadd www
usermod -G www www
mkdir -p /data/app/nginx
mkdir -p /data/www
mkdir -p /data/download
mkdir -p /data/logs
chown www.www /data/logs
mkdir -p /data/logs/www
chown www.www /data/logs/www
mkdir -p /data/logs/nginx
chown www.www /data/logs/nginx
编译安装nginx
cd /data/download/
wget http://nginx.org/download/nginx-1.18.0.tar.gz
tar -zxvf nginx-1.18.0.tar.gz
cd nginx-1.18.0
./configure --prefix=/data/app/nginx --user=www --group=www --with-http_ssl_module --with-http_v2_module --with-pcre --with-pcre-jit
make && make install
/data/app/nginx/sbin/nginx -v
配置多个站点
cp /data/app/nginx/conf/nginx.conf /data/app/nginx/conf/nginx.conf.bak
cat > /data/app/nginx/conf/nginx.conf << EOF
user www www;
worker_processes 1;
error_log /data/logs/nginx/nginx_error.log warn;
pid /var/run/nginx.pid;
worker_rlimit_nofile 65535;
events {
use epoll;
worker_connections 10240;
multi_accept on;
}
http {
include mime.types;
default_type application/octet-stream;
charset utf8;
server_tokens off;
server_name_in_redirect off;
server_names_hash_bucket_size 128;
client_header_buffer_size 16k;
large_client_header_buffers 4 16k;
client_max_body_size 50m;
client_body_buffer_size 128K;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 128k;
fastcgi_buffers 32 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 156k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 6;
gzip_types text/plain application/javascript application/x-javascript text/javascript text/css application/xml application/xml+rss;
log_format snlog '\$remote_addr | \$remote_user | \$time_local | \$request | \$status | '
'\$bytes_sent | \$body_bytes_sent | \$host | \$http_referer | \$http_user_agent | '
'\$upstream_addr | \$gzip_ratio | \$http_x_forwarded_for | \$request_time | \$upstream_response_time';
access_log off;
open_file_cache max=65535 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
open_file_cache_errors on;
proxy_connect_timeout 300;
proxy_send_timeout 300;
proxy_read_timeout 300;
proxy_buffering on;
proxy_buffer_size 32k;
proxy_buffers 128 32k;
proxy_busy_buffers_size 256k;
proxy_temp_file_write_size 256k;
upstream unix__tmp_php_cgi_sock {
server unix:/tmp/php-cgi.sock weight=100 max_fails=5 fail_timeout=30;
server unix:/tmp/php-cgi2.sock weight=100 max_fails=5 fail_timeout=30;
server unix:/tmp/php-cgi3.sock weight=100 max_fails=5 fail_timeout=30;
}
# 禁止未授权访问
server {
listen 80 default_server;
server_name _;
location / {
return 444;
}
access_log off;
}
include vhost/*.conf;
}
EOF
mkdir -p /data/app/nginx/conf/vhost
cat > /data/app/nginx/conf/vhost/default.conf << EOF
server {
listen 80;
server_name www.host.com www.host1.com;
index index.html index.htm index.php;
root /data/www/default;
# 配置tp框架隐藏index
location / {
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=/$1 last;
break;
}
index index.html index.htm index.php;
}
location ~ /\. {
deny all;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
location ~ \.php$
{
index index.php;
try_files $uri =404;
fastcgi_pass unix__tmp_php_cgi_sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
#access_log /data/logs/www/host.com.log snlog;
}
#server {
# #443端口
# listen 443 ssl;
# #域名
# server_name ~^(?<subdomain>.+).host.com;
# #证书和key目录
# ssl_certificate /data/www/default/ssl/host.com.pem;
# ssl_certificate_key /data/www/default/ssl/host.com.key;
#
# index index.html index.htm index.php;
# #指向的网页文件
# root /data/www/default;
# # 配置tp框架隐藏index
# location / {
# if (!-e $request_filename) {
# rewrite ^(.*)$ /index.php?s=$1 last;
# break;
# }
# }
#
# location ~ \.php$
# {
# index index.php;
# try_files $uri =404;
# fastcgi_pass unix__tmp_php_cgi_sock;
# include fastcgi_params;
# fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# fastcgi_param SCRIPT_NAME $fastcgi_script_name;
# }
# #Log生成位置
# #access_log /data/logs/www/*.host.com.log snlog;
#}
#http自动跳转https
# server {
# listen 80;
# server_name *.host.com;
# rewrite ^(.*) https://*.host.com$1 permanent;
# }
EOF
启动配置
/data/app/nginx/sbin/nginx -t
/data/app/nginx/sbin/nginx
/data/app/nginx/sbin/nginx -s stop
/data/app/nginx/sbin/nginx -s reload
vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/data/app/nginx/sbin/nginx
ExecReload=/data/app/nginx/sbin/nginx -s reload
ExecStop=/data/app/nginx/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[Unit]
Description=nginx service
After=network.target
[Service]
Type=forking
ExecStart=/data/app/nginx/sbin/nginx
ExecReload=/data/app/nginx/sbin/nginx -s reload
ExecStop=/data/app/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
systemctl start nginx.service
systemctl enable nginx.service
开机启动方法二
vim /etc/init.d/nginx
cat > /etc/init.d/nginx << EOF
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/data/app/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
killall -9 nginx
}
restart() {
configtest || return $?
stop
sleep 1
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
EOF
chmod +x /etc/init.d/nginx
chkconfig --add nginx
chkconfig nginx on
service nginx start