LNMP-Nginx启动脚本、配置文件

一、Nginx除了bin目录下默认的启动方式和设置变量启动服务之外,我们也可以自己编写启动脚本放置在init.d目录下,让其开机自启服务!
 
1:编写启动脚本
[root@ghs ~]# vim /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
 
2:设置权限
[root@ghs ~]# chmod 755 /etc/init.d/nginx
 
3:加入系统服务,并开机自动启动服务
[root@ghs ~]# chkconfig --add nginx
 [root@ghs ~]# chkconfig nginx on
 
5:测试启动脚本是否能启动
[root@ghs ~]# service nginx start
正在启动 Nginx:                                           [确定]
 
 
 
二、通常情况下,服务器上Nginx服务,都会同时跑一个或者N+个网站,这时候需要我们运维人员自定义编写conf文件,Nginx默认配置文件是没有开启虚拟主机配置,加入include指定虚拟主机conf文件存放路径,开启虚拟主机!
 
1:编写conf文件
[root@ghs ~]# vim /usr/local/nginx/conf/nginx.conf
 
user nobody nobody;
worker_processes 2;
error_log /usr/local/nginx/logs/nginx_error.log crit;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;
 
events
{
    use epoll;
    worker_connections 6000;
}
 
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;
    include vhosts/*.conf;
 }
 
2:切换目录
[root@ghs nginx]# cd /usr/local/nginx/conf/
 
3:创建虚拟主机目录vhosts
[root@ghs conf]# mkdir vhosts
 
4:编写配置虚拟主机文件
[root@ghs vhosts]# vim /vhosts/default.conf
 
server
{
    listen 80 default_server;
    server_name localhost;
    index index.html index.htm index.php;
    root /tmp/123;这个是不存在的实验用的
 
 
}
 
5:创建WEB服务目录
[root@ghs vhosts]# cd /tmp/
[root@ghs tmp]# mkdir 123
 
 
6:命令加-t参数,检查虚拟主机配置是否有误
[root@ghs vhosts]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
 
 
7:重新加载服务
[root@ghs vhosts]# nginx -s reload
 
 
8:curl命令测试
[root@ghs vhosts]# curl localhost
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.14.2</center>
</body>
</html>
 

猜你喜欢

转载自www.cnblogs.com/douyi/p/11578873.html