Linux下搭建Nginx服务器
Nginx 安装
一. 安装编译工具及库文件
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
二. 安装 PCRE(PCRE 作用是让 Nginx 支持 rewrite 功能)
1. 下载 PCRE 安装包,下载地址: https://sourceforge.net/projects/pcre/files/pcre/8.42/pcre-8.42.tar.gz
可以去网页下载也可以直接用命令下载:
wget https://sourceforge.net/projects/pcre/files/pcre/8.42/pcre-8.42.tar.gz
2. 解压安装包到指定目录:
tar -zxvf pcre-8.35.tar.gz -C /usr/local/src
3.编译安装pcre
cd /usr/local/src/pcre-8.3.5
./configure
make && make install
pcre-8.35.tar.gz
4. 查看pcre版本
pcre-config --version
三. 安装 Nginx
1. 下载 Nginx,下载地址:http://nginx.org/download/nginx-1.14.0.tar.gz
可以去网页下载也可以直接用命令下载:
wget http://nginx.org/download/nginx-1.14.0.tar.gz
2. 解压安装包到指定目录:
tar -zxvf nginx-1.14.0.tar.gz -C /usr/local
3.编译安装nginx
cd /usr/local/nginx-1.14
#检查安装环境,并指定将来要安装的路径
./configure --prefix=/usr/local/nginx
make && make install
pcre-8.35.tar.gz
4. 查看nginx版本
cd /usr/local/nginx/sbin
./nginx -v
四. Nginx 配置
#进入配置文件目录
cd /usr/local/nginx/conf
#打开并修改配置文件
vi 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; 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$ { # 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; # server_name localhost; # ssl on; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_timeout 5m; # ssl_protocols SSLv2 SSLv3 TLSv1; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }
将其中的域名、站点目录修改为我们需要的地址即可。
二. Nginx的常用命令
#切换目录到指定目录
cd /usr/local/nginx/sbin
1 ./nginx -s quit 优雅的停止nginx,有连接时会等连接请求完成再杀死worker进程 2 3 ./nginx -s reload 优雅的重启,并重新载入配置文件nginx.conf 4 5 ./nginx -s stop 6 7 ./nginx -s reopen 重新打开日志文件,一般用于切割日志 8 9 ./nginx -v 查看版本 10 11 ./nginx -t 检查nginx的配置文件 12 13 ./nginx -h 查看帮助信息 14 15 ./nginx -V 详细版本信息,包括编译参数 16 17 ./nginx -c filename 指定配置文件 18 19 quit 是一个优雅的关闭方式,Nginx在退出前完成已经接受的连接请求。 20 21 22 stop 是快速关闭,不管有没有正在处理的请求。