Nginx-基本安装
1.安装编译工具及库文件
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
2.安装PCRE
PCRE的作用是让Nginx支持Rewrite功能。
- 下载PCRE安装包
cd /opt
wget https://nchc.dl.sourceforge.net/project/pcre/pcre/8.44/pcre-8.44.tar.gz --no-check-certificate
- 解压安装包
tar zxvf pcre-8.44.tar.gz
- 进入安装包目录
cd pcre-8.44
- 编译安装
./configure
make && make install
- 查看pcre版本
pcre-config --version
3.安装Nginx
nginx网址(如果文档中下载的版本有问题可自行下载新版):
https://nginx.org/en/download.html
- 下载Nginx
cd /opt
wget https://nginx.org/download/nginx-1.24.0.tar.gz
- 解压安装包
tar zxvf nginx-1.24.0.tar.gz
- 进入解压后文件目录
cd nginx-1.24.0
- 编译安装(注意命令中–prefix=后跟的路劲为nginx编译安装后的服务器文件目录,–with-pcre=后的路劲为安装pcre的目录)
./configure --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/opt/pcre-8.44
make && make install
- 查看Nginx版本
/usr/local/webserver/nginx/sbin/nginx -v
4.Nginx配置
- 创建Nginx运行使用的用户nginx
groupadd nginx
useradd -g nginx nginx
chown -R nginx:nginx /usr/local/webserver/nginx
- 配置nginx.conf
#user nobody;
user nginx nginx; #定义运行的用户和组
worker_processes 1; #进程数,建议与CPU数相同#error_log logs/error.log;
error_log /usr/local/webserver/nginx/logs/nginx_error.log crit; #设置日志位置和日志级别,由高到低,[ debug | info | notice | warn | error | crit ]#error_log logs/error.log notice;
#error_log logs/error.log info;#pid logs/nginx.pid;
pid /usr/local/webserver/nginx/nginx.pid; #进程pid文件worker_rlimit_nofile 65535; #指定进程可以打开的最多文件数目events {#use epoll; #参考事件模型,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ],建议使用epollworker_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; #sendfile指令指定 nginx 是否调用sendfile 函数(zero copy 方式)来输出文件,对于普通应用,必须设为on。如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络IO处理速度,降低系统uptime。#tcp_nopush on; #此选项允许或禁止使用socke的TCP_CORK的选项,此选项仅在使用sendfile的时候使用#keepalive_timeout 0; 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;try_files $uri $uri/ /index.html; #解决部署后刷新页面返回错误页面问题}#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;#}}# 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;# location / {# root html;# index index.html index.htm;# }#}}
- 检查配置文件nginx.conf的正确性命令
/usr/local/webserver/nginx/sbin/nginx -t
5.启动Nginx
- 启动
/usr/local/webserver/nginx/sbin/nginx
- 查看状态
ps -ef|grep nginx
6.访问站点
http://172.30.250.21/
7.Nginx的其他常用命令
/usr/local/webserver/nginx/sbin/nginx -s reload # 重新载入配置文件
/usr/local/webserver/nginx/sbin/nginx -s reopen # 重启 Nginx
/usr/local/webserver/nginx/sbin/nginx -s stop # 停止 Nginx