Centos 7.5 编译安装Nginx

  • Post author:
  • Post category:其他


安装依赖包

[root@nginx ~]# yum -y install gcc gcc-c++ make libtool zlib zlib-devel pcre pcre-devel openssl openssl-devel

创建nginx用户

[root@nginx ~]# useradd -s /sbin/nologin www

编译安装nginx

nginx包下载地址:

http://nginx.org/download/nginx-1.18.0.tar.gz

[root@nginx ~]# tar zxf nginx-1.18.0.tar.gz 
[root@nginx ~]# cd nginx-1.18.0/
[root@nginx nginx-1.18.0]# ./configure --prefix=/usr/local/nginx1.18 --user=www --group=www --with-http_stub_status_module --with-http_realip_module --with-http_ssl_module --with-http_gzip_static_module  --with-pcre  --with-http_flv_module

[root@nginx nginx-1.18.0]# make && make install

优化nginx执行路径

[root@nginx nginx-1.18.0]# ln -s /usr/local/nginx1.18/sbin/nginx /usr/local/sbin/
[root@nginx nginx-1.18.0]# nginx -t
nginx: the configuration file /usr/local/nginx1.18/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx1.18/conf/nginx.conf test is successful

编写nginx脚本,内容如下:

[root@nginx ~]# vim /etc/init.d/nginx 
#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# chkconfig: - 85 15
# pidfile: /usr/local/nginx1.18/logs/nginx.pid
# config: /usr/local/nginx1.18/conf/nginx.conf
nginxd=/usr/local/nginx1.18/sbin/nginx
nginx_config=/usr/local/nginx1.18/conf/nginx.conf
nginx_pid=/usr/local/nginx1.18/logs/nginx.pid
RETVAL=0
prog="nginx"
# Source function library.
. /etc/rc.d/init.d/functions
# Start nginx daemons functions.
start() {
if [ -f $nginx_pid ] ; then
echo "nginx already running...."
exit 1
fi
echo -n "Starting $prog: "
   $nginxd -c ${nginx_config}
   RETVAL=$?
[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
}
# Stop nginx daemons functions.
stop() {
echo -n "Stopping $prog: "
        $nginxd -s stop
        RETVAL=$?
[ $RETVAL = 0 ] &&rm -f /var/lock/subsys/nginx
}
# reloadnginx service functions.
reload() {
echo -n "Reloading $prog: "
    $nginxd -s reload
}
# statusngnx service functions
status() {
if [ -f $nginx_pid ] ; then
echo  "$prog is running"
else
echo  "$prog is stop"
fi
}
case "$1" in
start)
start
        ;;
stop)
stop
        ;;
reload)
reload
        ;;
restart)
stop
start
        ;;
status)
status
        ;;
*)
echo "Usage: $prog {start|stop|restart|reload|status}"
exit 1
        ;;
esac

给与脚本执行权限,并设置为开机自启

[root@nginx ~]# chmod +x /etc/init.d/nginx 
[root@nginx ~]# chkconfig --add nginx
[root@nginx ~]# chkconfig nginx on
[root@nginx ~]# systemctl daemon-reload 

启动Nginx服务

[root@nginx ~]# systemctl start nginx
[root@nginx ~]# systemctl status nginx
● nginx.service - (null)
   Loaded: loaded (/etc/rc.d/init.d/nginx; bad; vendor preset: disabled)
   Active: active (running) since 日 2023-01-29 11:19:47 CST; 11s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 18417 ExecStop=/etc/rc.d/init.d/nginx stop (code=exited, status=0/SUCCESS)
  Process: 19017 ExecStart=/etc/rc.d/init.d/nginx start (code=exited, status=0/SUCCESS)
 Main PID: 19021 (nginx)
   CGroup: /system.slice/nginx.service
           ├─19021 nginx: master process /usr/local/nginx1.18/sbin/nginx -c /usr/local/nginx1.18/conf/nginx.conf
           └─19022 nginx: worker process

1月 29 11:19:47 nginx systemd[1]: Starting (null)...
1月 29 11:19:47 nginx nginx[19017]: Starting nginx:
1月 29 11:19:47 nginx systemd[1]: Started (null).
[root@nginx ~]# netstat -anptu | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      19021/nginx: master 

访问验证

nginx访问状态统计

编辑nginx.conf文件

[root@nginx ~]# vim /usr/local/nginx1.18/conf/nginx.conf
location /status {
                stub_status on;
                access_log off;
        }

重启服务

[root@nginx ~]# systemctl restart nginx.service 

访问测试

显示如下内容表示成功

基于域名的虚拟主机

准备网页

[root@nginx ~]# mkdir -p /var/www/google
[root@nginx ~]# mkdir -p /var/www/baidu
[root@nginx ~]# echo "<h1>www.google.com</h1>" > /var/www/google/index.html
[root@nginx ~]# wget www.baidu.com -O /var/www/baidu/index.html
--2023-02-03 11:47:41--  http://www.baidu.com/
正在解析主机 www.baidu.com (www.baidu.com)... 110.242.68.3, 110.242.68.4
正在连接 www.baidu.com (www.baidu.com)|110.242.68.3|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:2381 (2.3K) [text/html]
正在保存至: “/var/www/baidu/index.html”

100%[====================================================================================================>] 2,381       --.-K/s 用时 0s      

2023-02-03 11:47:41 (346 MB/s) - 已保存 “/var/www/baidu/index.html” [2381/2381])

调整nginx.conf配置文件(把server区域复制一份修改即可)

server {
        listen       80;
        server_name  www.baidu.com;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;

        location / {
            root   /var/www/baidu;
            index  index.html index.htm;
        }

        location /status {
                stub_status on;
                access_log off;
        }
}
 server {
        listen       80;
        server_name  www.google.com;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;

        location / {
            root   /var/www/google;
            index  index.html index.htm;
        }

        location /status {
                stub_status on;
                access_log off;
        }
}

添加完毕后,使用nginx -t检查配置是否有问题

[root@nginx ~]# nginx -t
nginx: the configuration file /usr/local/nginx1.18/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx1.18/conf/nginx.conf test is successful

修改客户端主机hosts文件

访问测试



版权声明:本文为wuzhenxu1995原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。