一、简介
nginx是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP代理服务器。它的代码完全用c语言写成。关于nginx的原理可以看这篇文章:https://www.jianshu.com/p/6215e5d24553
二、准备
1、redhat7.2镜像
2、安装编译工具和库文件:
# yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
3、安装pcre
1)pcre下载地址:http://ftp.pcre.org/pub/pcre/
2)解压安装包
# tar zxvf pcre-8.35.tar.gz
3)编译安装
# cd pcre-8.35/
# make && make install
三、安装
1、下载nginx安装包:http://nginx.org/download/
2、解压
# tar zxvf nginx-1.10.2.tar.gz
3、编译
# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre
4、安装
# make
# make install
5、测试
# /usr/local/nginx/sbin/nginx -v
[root@localhost nginx-1.10.2]# /usr/local/nginx/sbin/nginx -v
nginx version: nginx/1.10.2
此上,我们已安装成功,接下来我们看一下配置。
四、配置
1、创建用户
# /usr/sbin/groupadd ruready
# /usr/sbin/useradd -g ruready ruready
2、配置nginx.conf文件
# vi /usr/local/nginx/conf/nginx.conf
user ruready ruready;
worker_processes 2;
error_log /usr/local/nginx/logs/error.log crit; # 日志位置和日志级别
pid /usr/local/nginx/logs/nginx.pid;
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"';
access_log /usr/local/nginx/logs/access.log main;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
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;
# 下面是server虚拟主机的配置
server {
listen 80;#监听端口
server_name localhost;#域名
charset utf-8;
access_log /usr/local/nginx/logs/host.access.log main;
location / {
}
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;
# }
#}
3、检查配置文件的正确性
# /usr/local/nginx/sbin/nginx -t
[root@localhost ~]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
五、启动
1、启动命令
# /usr/local/nginx/sbin/nginx
[root@localhost ~]# /usr/local/nginx/sbin/nginx
[root@localhost ~]# ps -ef | grep nginx
root 56187 1 0 03:31 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
ruready 56188 56187 7 03:31 ? 00:00:01 nginx: worker process
ruready 56189 56187 4 03:31 ? 00:00:01 nginx: worker process
root 56202 55870 0 03:32 pts/1 00:00:00 grep --color=auto nginx
2、访问测试
我的虚拟机的ip是:192.168.100.160,所以输入http://192.168.100.160,即可访问。
还有一些其他的操作可以参考大佬的博客:https://www.linuxidc.com/Linux/2017-05/143938.htm