Centos7 安装、配置、卸载 nginx 步骤解析

Centos7 安装、配置、卸载 nginx 步骤解析

 

本文结构

    • 1、安装依赖环境

    • 2、安装nginx

    • 3、nginx配置

    • 4、启动nginx

    • 5、卸载nginx

    • 6、查看nginx的进程

    • 7、配置开机自启动

    • 8、端口开放

 

Centos下安装nginx步骤解析

在linux下配置nginx服务器步骤

1、安装依赖环境

  • 安装gcc环境

    yum install gcc-c++
  • 安装PCRE库,用于解析正则表达式,让nginx有rewrite功能

    yum install -y pcre pcre-devel
    #查看安装的版本
    pcre-config --version
  • zlib压缩和解压缩依赖

    yum install -y zlib zlib-devel
  • SSL 安全的加密的套接字协议层,用于HTTP安全传输,也就是https

    yum install -y openssl openssl-devel

     

2、安装nginx

  1. 下载nginx:

    # 第一种:使用wget直接拉取nginx压缩文件
    wget [http://nginx.org/download/nginx-1.6.2.tar.gz](https://nginx.org/download/nginx-1.6.2.tar.gz)

    #第二种:从官网下载压缩包并上传nginx压缩包到Centos
  2. 解压安装包:

    tar -zxvf nginx-1.6.2.tar.gz  #解压
  3. 进入安装包目录:

    cd nginx-1.6.2  #切换目录
  4. 编译安装:

    ./configure  #默认编译安装地址 /usr/local/nginx

    ./configure --prefix=/usr/local/webserver/nginx #自定义指定编译安装地址
  5. 安装:

    make
  6. 安装:

    make install
 注:
  第一次编译的时候:
  使用64位的系统第一次编译安装出现
  error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory这种情况,
  nginx默认是在lib64下,32为的是在lib下
  查找:find / -name 'libpcre.so.1' 出现 /usr/local/lib/libpcre.so.1,
  我们建立以符号链接:ln -s /usr/local/lib/libpcre.so.1 /lib64/libpcre.so.1
  这样可以查看nginx版本 /usr/local/nginx/sbin/nginx -v 出现版本号

3、nginx配置

查找安装路径下nginx.conf配置文件,默认安装的配置路径:cd /usr/local/nginx/conf ,把下面的内容覆盖到nginx.conf,内容从菜鸟网站上搜索的

#查找nginx配置文件所在位置
find / -name 'nginx.conf'

S222222

 

具体配置文件内容:

 user www www;
worker_processes 2; #设置值和CPU核心数一致
error_log /usr/local/nginx/logs/nginx_error.log crit; #日志位置和日志级别
pid /usr/local/nginx/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;
events
{
  use epoll;
  worker_connections 65535;
}
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';
 
#charset gb2312;
     
  server_names_hash_bucket_size 128;
  client_header_buffer_size 32k;
  large_client_header_buffers 4 32k;
  client_max_body_size 8m;
     
  sendfile on;
  tcp_nopush on;
  keepalive_timeout 60;
  tcp_nodelay on;
  fastcgi_connect_timeout 300;
  fastcgi_send_timeout 300;
  fastcgi_read_timeout 300;
  fastcgi_buffer_size 64k;
  fastcgi_buffers 4 64k;
  fastcgi_busy_buffers_size 128k;
  fastcgi_temp_file_write_size 128k;
  gzip on;
  gzip_min_length 1k;
  gzip_buffers 4 16k;
  gzip_http_version 1.0;
  gzip_comp_level 2;
  gzip_types text/plain application/x-javascript text/css application/xml;
  gzip_vary on;
 
  #limit_zone crawler $binary_remote_addr 10m;
#下面是server虚拟主机的配置
server
  {
    listen 80;#监听端口
    server_name localhost;#域名
    index index.html index.htm index.php;
    root /usr/local/nginx/html;#站点目录
      location ~ .*\.(php|php5)?$
    {
      #fastcgi_pass unix:/tmp/php-cgi.sock;
      fastcgi_pass 127.0.0.1:9000;
      fastcgi_index index.php;
      include fastcgi.conf;
    }
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$
    {
      expires 30d;
  # access_log off;
    }
    location ~ .*\.(js|css)?$
    {
      expires 15d;
  # access_log off;
    }
    access_log off;
  }
}

可以检测配置的是否正确

image-20201213131956268

/usr/local/nginx/sbin/nginx -t

  说明配置成功

4、启动nginx

启动的时候有报错,如下

nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
  nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
  nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
  nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)…

这说明80接口有被占用,查看接口

netstat -ntpl #查询所有端口使用情况
kill -9 $pid #杀掉进程

netstat -ntpl | grep 8888 #查询指定端口的占用情况
kill -9 $pid #杀掉进程

再次启动

/usr/local/nginx/sbin/nginx

停止服务器

/usr/local/nginx/sbin/nginx -s stop 或 /usr/local/nginx/sbin/nginx -s quick

网页访问 127.0.0.1 image-20201213131918366  至此,安装配置完成!

在学习过程中配到问题:

1、在下载安装pcre和nginx中,要用root身份,一般下载到/usr/local/src/,下载到其他地方配置呢?(测试几次没成功)

2、菜鸟网站中编译nginx的时候 配置安装地址

./configure --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.35

编译不成功?(配置安装路径应该没问题,编译的时候报pcrelib.so.0不存在,安装的时候webserver文件没有建立成功,想着应该还是和64位的配置文件路径有关系,有待验证)

5、卸载nginx

删除nginx文件即可

#1、查找CentOS系统中所有nginx文件
find / -name 'nginx'
# 2、根据实际情况卸载nginx(即删除所有nginx目录)
rm -rf /usr/local/nginx

6、查看启动的进程

ps -ef | grep nginx #查看运行的nginx进程

7、配置开机自启动

切换到/lib/systemd/system/目录,创建nginx.service文件vim nginx.service

#1、切换系统服务目录
cd /lib/systemd/system/ 

#2、创建并编辑nginx.service
# vim nginx.service

文件内容如下:

[Unit]
Description=nginx
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx reload
ExecStop=/usr/local/nginx/sbin/nginx quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target

退出并保存文件,执行systemctl enable nginx.service使nginx开机启动

# systemctl enable nginx.service

 

systemctl enable nginx.service  设置开机启动
systemctl start nginx.service   启动nginx服务
systemctl enable nginx.service  设置开机自启动
systemctl disable nginx.service 停止开机自启动
systemctl status nginx.service  查看服务当前状态
systemctl restart nginx.service 重新启动服务
systemctl list-units --type=service 查看所有已启动的服务

8、端口开放

说明:安装的nginx监听端口,如防火墙未对外开放,则远程无法访问到nginx,需要按如下步骤开放对应的监听端口

a、安装防火墙

 $ yum install -y firewalld

b、 基本启动命令

 $ systemctl status firewalld        # 查看防火墙服务状态
 $ systemctl start firewalld         # 启动防火墙服务
 $ systemctl stop firewalld          #关闭防火墙服务
 $ systemctl enable firewalld        # 开机启动防火墙服务
 $ systemctl disable firewalld       # 取消开机启动防火墙服务

c、常用命令

1). 查看

 $ firewall-cmd --get-active-zones   # 查看激活的域
 $ firewall-cmd --zone=public --list-ports  # 查看开放的端口
 $ firewall-cmd --zone=public --list-rich-rules  # 查看添加的规则

2). 添加端口

 # 开放单个端口
 $ firewall-cmd --zone=public --add-port=80/tcp --permanent                     
 
 # 开放端口范围
 $ firewall-cmd --zone=public --add-port=8388-8389/tcp --permanent        
 
 # 对 147.152.139.197 开放10000端口
 $ firewall-cmd --permanent --zone=public --add-rich-rule='
 rule family="ipv4"
 source address="147.152.139.197/32"
 port protocol="tcp" port="10000" accept'       
 
 # 拒绝端口:
 $ firewall-cmd --permanent --zone=public --add-rich-rule='
 rule family="ipv4"
 source address="47.52.39.197/32"
 port protocol="tcp" port="10000" reject'  
 
 # 开放全部端口给IP
 $ firewall-cmd --permanent --zone=public --add-rich-rule='
 rule family="ipv4"
 source address="192.168.0.1/32" accept';
 
 # 开放全部端口给网段
 $ firewall-cmd --permanent --zone=public --add-rich-rule='
 rule family="ipv4"
 source address="192.168.0.0/16" accept';

3). 添加服务

 # 查看全部支持的服务
 $ firewall-cmd --get-service
 
 # 查看开放的服务
 $ firewall-cmd --list-service
 
 # 添加服务,添加https
 $ firewall-cmd --add-service=https --permanent

修改对应的配置文件是/etc/firewalld/zones/public.xml

4). 移除端口

 # 移除添加的端口
 $ firewall-cmd --zone=public --remove-port=80/tcp --permanent      

5). 重载配置

对路由规则进行修改后,需要重新加载规则才能使规则生效

$ firewall-cmd --reload  #重载配置

 

来源: https://www.pianshen.com/article/4615406674/

posted @   Edwin_Lee  阅读(693)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示