nginx安装 | 设置软连接 | 完全卸载

1. 下载并安装

从nginx官网下载安装包,然后把下载好的安装包上传到刚创建的文件夹

mkdir /data0

进入到文件夹并解压安装包

cd /data0 && tar -xvf nginx-1.16.1.tar.gz

进入解压好的文件夹并执行配置文件

cd nginx-1.16.1 && ./configure

执行make进行编译

make

执行make install进行安装

make install

到这里安装就算是完成了 这时候在文件夹nginx-1.16.1下有那么几个文件夹

  • conf配置有关的文件夹
  • objs启动有关的文件夹

进入objs会发现有一个文件是绿色的,也就是nginx的文件夹,这个就是启动nginx的文件只要执行一下命令就会启动nginx

./nginx

但是只执行以上命令,你会发现所有的配置用的都是nginx默认的配置,如果想用自己设置的配置进行启动只需要加上配置文件的地址即可

./nginx -c /data0/nginx-1.16.1/conf/nginx.conf

nginx文件夹conf配置文件夹

nginx的配置文件是在conf文件夹下名为nginx.conf的文件

我们可以看下默认的配置

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    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;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

默认监听的是80端口,也就是启动的端口.

2. 建立软连接

nginx启动比较麻烦,每次都需要执行

/data0/nginx-1.16.1/objs/nginx -c /data0/nginx-1.16.1/conf/nginx.conf

所以建立软连接,方便我们对nginx的操作

首先我们要创建一个启动脚本

vi /etc/init.d/nginx

然后将下面的代码贴到里面保存(nginx路径要换成自己的nginx启动的文件路径 | 配置NGINX_CONF_FILE路径也要换成自己的配置文件的路径)

#!/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启动的路径
    nginx="/usr/local/nginx/sbin/nginx"   
    prog=$(basename $nginx)   
    #自己的nginx的配置文件路径
    NGINX_CONF_FILE="/usr/local/nginx/conf/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   

赋予脚本 755 权限

  • 第一个7,文件的拥有者可读(4) 可写(2) 可执行(1)
  • 第一个5,文件所拥有的组可读(4) 不可写(0) 可执行(1)
  • 第二个5,公共用户的权限可读(4) 不可写(0) 可执行(1)
chmod 755 /etc/init.d/nginx

将脚本添加到chkconfig中

chkconfig --add /etc/init.d/nginx

设置开机在3和5级别自动启动

chkconfig --level 35 nginx on

创建软连接

cd /usr/bin
ln -s /etc/init.d/nginx

常用的命令

nginx start 	#启动
nginx stop		#关闭
nginx restart 	#重启

[]: https://www.cnblogs.com/chenhaoyu/p/8899816.html "感谢这位博主的分享"

3. 完全卸载

卸载前我们需要将nginx停止

我们先找到nginx运行的PID,然后使用kill -9把进程杀死

ps -aux|grep nginx #找到nginx 进程号
kill -9 PID

如果不知道nginx的安装路径可以使用一下命令找到nginx安装路径

find / -name nginx

然后使用一下命令逐个删除即可,如果前缀大部分相同,那后面的可以使用 * 代替,方便快速删除

rm -rf 文件夹路径

如果设置了开机自启和弄了软连接

chkconfig nginx off
rm -rf /etc/init.d/nginx

[]: https://blog.csdn.net/weixin_38889300/article/details/106682750 "感谢这位博主"

posted @ 2021-10-08 10:41  _hsz366  阅读(977)  评论(0)    收藏  举报