搭建个人服务器——安装nginx(离线)

前言

本文提供两种nginx的安装方式,并对每一种做了简单说明。
下载离线包:http://nginx.org/packages/centos/7/x86_64/RPMS/

第一种nginx离线安装方式

该方式安装的nginx的目录文件比较分散,并且是nginx的安装目录会是默认的,但是它也是最省事的一种安装方式
# 安装开始
   将下载好的nginx-1.16.1-1.el7.ngx.x86_64.rpm 上传至服务器:/usr/local/nginx
# 将下载的rpm包上传到服务器,然后赋予可执行权限,执行安装:
   sudo yum install -y nginx-1.16.1-1.el7.ngx.x86_64.rpm
# 查看安装的版本
   nginx -v 

# 启动或停止 
   service nginx start 启动,
   service nginx stop 停止 

>>> 安装结束
    下面配置nginx的代理信息以及负载均衡,然后重启即可

第二种nginx离线安装方式

# 安装开始  
    将下载好的nginx-1.18.0.tar.gz 上传至服务器:/usr/local/nginx
    
# 解压:    
    tar -zxvf nginx-1.18.0.tar.gz
    
# 将解压后的文件内的所有文件 移动到 /usr/local/nginx
    cd /usr/local/nginx-1.18.0
    mv * ../nginx
    
# 添加nginx到用户组
    groupadd -r nginx
    useradd -r -g nginx -s /bin/false -M nginx    
        
# 安装nginx依赖包:
    yum -y install zlib zlib-devel openssl openssl--devel pcre pcre-devel
    
# 开始安装: -- 目前nginx的安装位置: /usr/local/nginx/nginx-1.18.0
    cd  /usr/local/nginx/
# 执行./configure命令: 如下命令将解决你很多不必要的问题
    ./configure --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --pid-path=/var/run/nginx.pid

>>> 会提示这个错误
    ./configure: error: C compiler cc is not found
>>> 解决办法(因为上面那条命令式重新编译nginx,需要C环境所以需要安装环境):
    yum -y install gcc gcc-c++ autoconf automake make       

# 编译并安装:
    make && make install 
    
#进入安装目录的sbin目录下
    cd /usr/local/nginx/sbin
    
# 启动:
    ./nginx 
>>> 启动时可能会包找不到 /usr/local/nginx/logs/error.log    /usr/local/nginx/logs/access.log  这样就手动创建就可以了
>
# 关闭
    ./nginx -s stop  
    
# 重启
    ./nginx -s reload  
        
>>>>>>>>>>>>>> 到此,nginx算是部署完成了。>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>  只不过每次的启动,停止比较麻烦 ,所以我又把nginx服务注册到linux服务列表去,之后使用  
	service nginx start  
	service nginx stop 
等命令启动 关闭

## 将nginx注册到linux服务列表
# 创建服务脚本
  sudo vim /etc/init.d/nginx
# 脚本内容如下:

		#! /bin/sh
		# chkconfig: - 85 15
		
		PATH=/usr/local/nginx/sbin
		
		DESC="nginx daemon"
		NAME=nginx
		DAEMON=/usr/local/nginx/sbin/$NAME
		CONFIGFILE=/usr/local/nginx/conf/$NAME.conf
		
		# 存放nginx进程的一个文件,一般刚安装时是没有,需要手动在/var/run/ 创建 nginx.pid文件即可
		PIDFILE=/var/run/$NAME.pid 
		
		SCRIPTNAME=/etc/init.d/$NAME
		
		set -e
		[ -x "$DAEMON" ] || exit 0
		
		do_start() {
          
   
		$DAEMON -c $CONFIGFILE || echo -n "nginx already running"
		}
		
		do_stop() {
          
   
		$DAEMON -s stop || echo -n "nginx not running"
		}
		
		do_reload() {
          
   
		$DAEMON -s reload || echo -n "nginx cant reload"
		}
		
		case "$1" in
		start)
		echo -n "Starting $DESC: $NAME"
		do_start
		echo "."
		;;
		stop)
		echo -n "Stopping $DESC: $NAME"
		do_stop
		echo "."
		;;
		reload|graceful)
		echo -n "Reloading $DESC configuration..."
		do_reload
		echo "."
		;;
		restart)
		echo -n "Restarting $DESC: $NAME"
		do_stop
		do_start
		echo "."
		;;
		*)
		echo "Usage: $SCRIPTNAME {start|stop|reload|restart}" >&2
		exit 3
		;;
		esac
		
		exit 0    

# 添加服务  
    chkconfig --add nginx
    
# 测试 启动 停止 重启 重载
    service nginx start
    service nginx stop
    service nginx restart
    service nginx reload  
      
# 测试结果- 权限不够
    chmod a+x /etc/init.d/nginx 

# 还可以添加开机自启动 -- 看需要添加    
    chkconfig nginx on

本文结束

经验分享 程序员 微信小程序 职场和发展