centos7(虚拟机)下安装nginx,及Nginx配置报错处理,Nginx常用命令

安装前准备

环境配置

gcc: yum install gcc-c++
pcre: yum install -y pcre pcre-devel
zlib: yum install -y zlib zlib-devel
openssl: yum install -y openssl openssl-devel

安装Nginx

下载: 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
编译: make  make install
# 启动、停止
进入sbin目录
cd /usr/local/nginx/sbin
启动
./nginx 
停止
# 此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程。
./nginx -s stop
​# 此方式停止步骤是待nginx进程处理任务完毕进行停止。
./nginx -s quit
# 热启动
nginx -s reload
# 检查配置文件
nginx -t

环境变量配置

# 打开环境变量所在的文件:
vim /etc/profile
# 在profile文件末尾,加上如下内容
#指向你的nginx的安装位置的 sbin 目录
PATH=$PATH:/usr/local/nginx/sbin
# 重新加载环境
source /etc/profile

报错及解决方案

nginx -s reload 后报 nginx: [error] open() “/usr/local/nginx/logs/nginx.pid” failed (2: No such file or directory)错误
vim /usr/local/nginx/conf/nginx.conf

打开 pid /usr/local/nginx/logs/nginx.pid;

touch /usr/local/nginx/logs/nginx.pid

检查并创建nginx.pid文件

nginx -s reload后报 nginx -s reload报错[error] invalid PID number “” in "/usr/local/nginx/logs/nginx.pid"错误
# 执行
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

Nginx 配置

#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
------------------------------------------------------------------------------
events {
    worker_connections  1024;
}

# http
------------------------------------------------------------------------------
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
------------------------------------------------------------------------------
	server {
	# server全局块
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

	# location
        location / {
            root   html;
            index  index.html index.htm;
        }
		location /api/ {
			# 使用 proxy_pass(固定写法)后面跟要代理服务器地址            
            proxy_pass http://192.168.2.102:8081;
        }
        #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;
        #}
	}
------------------------------------------------------------------------------
	# 可以配置多个server块	

}

注:这里我会拿出单独的一篇文章介绍。

Nginx常用命令

# 查询端口占用
lsof -i:80
netstat -natp |grep 80
# 解除nginx占用
pkill nginx
killall -9 nginx
killall nginx
# 查看线程
ps -ef|grep nginx
# 查询nginx防火墙状态
systemctl status firewalld
# 临时关闭防火墙
systemctl stop firewalld
# 永久关闭防火墙
systemctl disable firewalld
# 解压命令: 
unzip dist.zip
# 安装上传下载命令
yum -y install lrzsz
# 上传命令: 
rz
# 下载命令: 
sz + 文件名
# 创建文件夹命令: 
mkdir + 文件名
# 创建文件命令:
touch + 文件名
# 删除文件命令: 
rm -f +文件名
# 删除文件夹命令: 
rm -rf + 文件名
# 获取文件绝对路径命令: 
pwd
# 更改文件名: 
mv xx yy

这次分享就到这里了。希望能帮助到你。

你可能感兴趣的:(Nginx,nginx,运维,linux)