一、卸载原有nginx
检查是否存在 nginx(有的话需要卸载掉自带的)
whereis nginx
rm -rf [nginx文件路径]
二、下载nginx
官网下载nginx,官网地址:http://nginx.org/en/download.html
这里我下载的是1.24.0版本,大家按需下载对应稳定版即可
将压缩包上传到linux虚拟机中。
三、安装nginx
找到压缩包进行解压,可将压缩包上传至/home/nginx/下,进行解压
tar -zxvf nginx-1.24.0.tar.gz
1、切换到/home/nginx/nginx-1.24.0/下面,执行3个命令:
./configure
make
make install
2、因为需要PCRE库,所以安装pcre和pcre-devel:
3、如果安装pcre后仍出现如下错误,则提示你安装zlib-devel:
安装zlib-devel的命令是:
切换到/home/nginx目录下:
四、配置nginx
配置nginx的配置文件nginx.conf文件(文件在conf下),主要也就是端口。
可以按照自己服务器的端口使用情况来进行配置
ESC键,wq!强制保存并退出
五、启动nginx服务,并验证
切换到目录/usr/local/nginx/sbin下面
启动nginx命令:
./nginx
查看nginx服务是否启动成功
ps -ef | grep nginx
访问你的服务器IP
说明安装和配置都没问题OK了
六、nginx.conf的配置说明
#user nobody;
worker_processes 1; #工作进程:数目。根据硬件调整,通常等于cpu数量或者2倍cpu数量。
#错误日志存放路径
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid; # nginx进程pid存放路径
events {
worker_connections 1024; # 工作进程的最大连接数量
}
http {
include mime.types; #指定mime类型,由mime.type来定义
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; #用log_format指令设置日志格式后,需要用access_log来指定日志文件存放路径
sendfile on; #指定nginx是否调用sendfile函数来输出文件,对于普通应用,必须设置on。
如果用来进行下载等应用磁盘io重负载应用,可设着off,以平衡磁盘与网络io处理速度,降低系统uptime。
#tcp_nopush on; #此选项允许或禁止使用socket的TCP_CORK的选项,此选项仅在sendfile的时候使用
#keepalive_timeout 0; #keepalive超时时间
keepalive_timeout 65;
#gzip on; #开启gzip压缩服务
#虚拟主机
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$ { #请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写。
# root html; #根目录
# fastcgi_pass 127.0.0.1:9000; #请求转向定义的服务器列表
# fastcgi_index index.php; # 如果请求的Fastcgi_index URI是以 / 结束的, 该指令设置的文件会被附加到URI的后面并保存在变量$fastcig_script_name中
# 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; # ssl_prefer_server_ciphers on; #
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
七、拓展
1、修改端口,将下面这个地方的80修改成自己想要的端口号如:81端口
2、重新加载修改的文件,并在防火墙中开启自定义端口
按下Esc 退出编辑模式
:wq # 保存并退出
./nginx -s reload #在刚刚的./sbin目录下重新加载该文件
firewall-cmd --zone=public --add-port=81/tcp --permanent # 开启防火墙端口81
systemctl restart firewalld.service #重启防火墙
3、本机再次访问 Linux IP :81
八、常用命令
./nginx -s stop #停止nginx
./nginx -s quit #安全退出
./nginx -s reload #修改了文件之后重新加载该程序文件
ps aux|grep nginx #查看nginx进程
[root@localhost nginx]# netstat -antp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 11042/nginx: master
tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN 1602/dnsmasq
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1024/sshd
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 1022/cupsd
tcp6 0 0 :::111 :::* LISTEN 1/systemd
tcp6 0 0 :::22 :::* LISTEN 1024/sshd
tcp6 0 0 ::1:631 :::* LISTEN 1022/cupsd
[root@localhost nginx]# kill -9 11042
九、开启防火墙,暴漏nginx端口
//开启端口访问
firewall-cmd --zone=public --add-port=80/tcp --permanent
命令含义: --zone #作用域 --add-port=80/tcp #添加端口,格式为:端口/通讯协议 --permanent #永久生效,没有此参数重启后失效。
//重启防火墙生效
systemctl restart firewalld.service
firewall-cmd --list-all 可以查看防火墙是否开启了80端口的访问
十、CentOS7下实现nginx开机自启动
1、在/etc/systemd/system下创建文件nginx.service
编辑nginx.service(以本地安装nginx路径:/usr/local/nginx为例)
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target/2重新加载所有服务
2、重新加载所有服务
systemctl daemon-reload/3设置开机自启动及其他命令
3、设置开机自启动及其他命令
# 开机自启动
systemctl enable nginx
#启动nginx
systemctl start nginx
#查看状态
systemctl status nginx
#停止nginx
systemctl stop nginx
#重启nginx
systemctl restart nginx
#额外命令,查看所有已启动的服务
systemctl list-units --type=service
本文暂时没有评论,来添加一个吧(●'◡'●)