nginx学习1:Linux安装nginx
nginx安装环境准备
1-安装pcre、openssl、zlib
2-安装指令:yum -y install gcc zlib zlib-devel pcre-devel openssl openssldevel
Installed: pcre-devel.x86_64 0:8.32-17.el7 zlib-devel.x86_64 0:1.2.7-18.el7 Updated: openssl.x86_64 1:1.0.2k-19.el7 zlib.x86_64 0:1.2.7-18.el7 Dependency Updated: openssl-libs.x86_64 1:1.0.2k-19.el7 Complete!
下载并安装nginx
1-官网下载nginx(本次下载的版本是nginx-1.12.2.tar),并上传到Linux服务器/opt目录下
2-在/opt下解压文件:先cd /opt;再 tar -xzvf nginx-1.12.2.tar.gz
3-执行nginx-1.12.2目录下的./configure:先cd nginx-1.12.2;再./configure
...........
Configuration summary + using system PCRE library + OpenSSL library is not used + using system zlib library nginx path prefix: "/usr/local/nginx" nginx binary file: "/usr/local/nginx/sbin/nginx" nginx modules path: "/usr/local/nginx/modules" nginx configuration prefix: "/usr/local/nginx/conf" nginx configuration file: "/usr/local/nginx/conf/nginx.conf" nginx pid file: "/usr/local/nginx/logs/nginx.pid" nginx error log file: "/usr/local/nginx/logs/error.log" nginx http access log file: "/usr/local/nginx/logs/access.log" nginx http client request body temporary files: "client_body_temp" nginx http proxy temporary files: "proxy_temp" nginx http fastcgi temporary files: "fastcgi_temp" nginx http uwsgi temporary files: "uwsgi_temp" nginx http scgi temporary files: "scgi_temp"
4-编译并安装nginx:make && make install
............ test -d '/usr/local/nginx/logs' \ || mkdir -p '/usr/local/nginx/logs' make[1]: Leaving directory `/opt/nginx-1.12.2'
5-(如果有需要)修改nginx监听端口(/usr/local/nginx/conf/下的nginx.conf 将listen 80 改为listen 8081):先cd /usr/local/nginx/conf/;再vim 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;
6-启动ngnix(执行 /usr/local/nginx/sbin/下的nginx脚本):先cd /usr/local/nginx/sbin/;再./nginx
8-查看nginx状态:lsof -i:8081
[root@VM_0_4_centos sbin]# lsof -i:8081 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME nginx 16148 root 6u IPv4 98149743 0t0 TCP *:tproxy (LISTEN) nginx 16149 nobody 6u IPv4 98149743 0t0 TCP *:tproxy (LISTEN)
检查防火墙是否允许对应的端口号开放
1-查看防火墙允许的端口号:firewall-cmd --list-all
如下异常:
FirewallD is not running
2-查看防火墙状态:systemctl status firewalld
防火墙dead ● firewalld.service - firewalld - dynamic firewall daemon Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled) Active: inactive (dead) Docs: man:firewalld(1)
3-启动防火墙:systemctl start firewalld (重新查看防火墙状态如下)
防火墙running ● firewalld.service - firewalld - dynamic firewall daemon Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled) Active: active (running) since Wed 2020-01-29 15:55:58 CST; 1min 54s ago Docs: man:firewalld(1) Main PID: 10829 (firewalld) Tasks: 2 Memory: 27.1M CGroup: /system.slice/firewalld.service └─10829 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid
4-继续查看防火墙允许的端口号:firewall-cmd --list-all
无端口号开放 public target: default icmp-block-inversion: no interfaces: sources: services: ssh dhcpv6-client ports: protocols: masquerade: no forward-ports: source-ports: icmp-blocks:
5-开放8081端口号:先firewall-cmd --add-port=8081/tcp --permanent;再firewall-cmd --reload
添加端口号 [root@VM_0_4_centos sbin]# firewall-cmd --add-port=8081/tcp --permanent success 重新加载防火墙 [root@VM_0_4_centos sbin]# firewall-cmd --reload success 查看开放端口 [root@VM_0_4_centos sbin]# firewall-cmd --list-all public target: default icmp-block-inversion: no interfaces: sources: services: ssh dhcpv6-client ports: 8081/tcp protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules:
浏览器访问nginx
如下成功