nginx服务一---nginx部署安装记录

来源:互联网 发布:种植牙 知乎 编辑:程序博客网 时间:2024/06/13 01:27
一、nginx介绍
nginx是一个高性能的 HTTP 服务器和反向代理服务器。nginx会按需同时运行多个进程:一个主进程(master)和几个工作进程(worker),配置了缓存时还
会有缓存加载器进程(cache loader)和缓存管理器进程(cache manager)等。所有进程均是仅含有一个线程,并主要通过“共享内存”的机制实现进程间通信。主进程以 root 用户身份运行,而worker、cache loader 和 cache manager 均应以非特权用户身份运行。
二、nginx部署
1、安装环境:操作系统  Centos6.7,yum源  阿里的epel和centos源
2、部署过程
(1)安装前准备
配置用户
useradd nginx -s /sbin/nologin
安装依赖
yum -y install gcc gcc-c++ automake autoconf libtool make
安装pcre
wget http://120.52.73.48/jaist.dl.sourceforge.net/project/pcre/pcre/8.37/pcre-8.37.tar.gz   -P  /usr/local/src/
tar zxf pcre-8.37.tar.gz
cd pcre-8.37
./configure
make && make install
安装zip
wget http://zlib.net/zlib-1.2.8.tar.gz -P /usr/local/src/
tar zxf zlib-1.2.8.tar.gz
cd zlib-1.2.8
./configure
make && make install
安装ssl
wget http://www.openssl.org/source/openssl-1.0.1p.tar.gz -P /usr/local/src/
cd /usr/local/src
tar zxf openssl-1.0.1p.tar.gz
cd openssl-1.0.1p
./config
make && make install
(2)开始安装nginx
mkdir -p /usr/local/nginx/temp
wget http://nginx.org/download/nginx-1.8.0.tar.gz
tar fxz nginx-1.8.0.tar.gz
cd nginx-1.8.0
./configure --prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--http-fastcgi-temp-path=/usr/local/nginx/temp/fastcgi \
--http-client-body-temp-path=/usr/local/nginx/temp/client \
--http-proxy-temp-path=/usr/local/nginx/temp/proxy \
--http-scgi-temp-path=/usr/local/nginx/temp/scgi \
--http-uwsgi-temp-path=/usr/local/nginx/temp/uwsgi \
--with-pcre=/usr/local/src/pcre-8.39 ##为解压后的源码包路径
--with-zlib=/usr/local/src/zlib-1.2.8 ##为解压后的源码包路径
--with-openssl=/usr/local/src/openssl-1.0.1p ##为解压后的源码包路径
make && make install
# 以下必须建立,否则在启动时报错#nginx:error while loading shared libraries:libpcre.so.1:cannot open shared object file: No such file or directory#
cd /lib64
ln -s libpcre.so.0.0.1 libpcre.so.1
(3)配置文件优化(可选,也可不进行配置)
主配置文件
cat > /usr/local/nginx/conf/nginx.conf << EOF
user nginx;
worker_processes 4;
worker_rlimit_nofile 65535;
events {
use epoll;
worker_connections 20480;
}
http {
include mime.types;
default_type application/octet-stream;
server_tokens off;
sendfile on;
keepalive_timeout 6;
include /usr/local/nginx/vhosts/*.conf;
}
EOF
虚机配置文件
mkdir -p /usr/local/nginx/vhosts
touch /usr/local/nginx/vhosts/****.conf        # 创建虚机的配置文件
cat > /opt/server/nginx/vhosts/****.conf << EOF
log_format main '\$http_x_forwarded_for - \$remote_addr [\$time_local] "\$request" '
'\$status \$body_bytes_sent "\$http_referer" ' '"\$http_user_agent" \$http_x_clientip';
server {
listen 80;
server_name  fengkong.puhuijia.com;
access_log     logs/fengkong.puhuijia.access.log  fengkong.puhuijia.com;
error_log         logs/fengkong.puhuijia.com.error.log error;
root       /app/fengkong/web; # 代码目录
location / {
index admin.php index.php index.html index.htm;
}
location ~ \.php\$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /app/fengkong/web\$fastcgi_script_name;
include fastcgi_params;
}
location ~ .*\.(git|svn|gitignore)\$ {
deny all;
}
}
EOF
三、nginx常用命令
1、启动&重启
/usr/local/sbin/nginx 启动
/usr/local/sbin/nginx -s stop 停止
/usr/local/sbin/nginx -t 检测配置文件
/usr/local/sbin/nginx -s reload 重新加载(重启)
2、查看版本
/usr/local/nginx/sbin/nginx -v