Nginx WEB 安装
首先要安装pcre库
yum -y install pcre-devel pcre
下载源码包
http://nginx.org/download/nginx-1.4.2.tar.gz
解压源码包
tar -xzvf nginx-1.4.2.tar.gz
进入Nginx的目录修改其版本
cd nginx-1.4.2 ; sed -i -e ‘s/1.4.2//g’ -e ‘s/nginx\//WS/g’ -e
‘s/”NGINX”/”WS”/g’ src/core/nginx.h
./configure 出错
./configure: error: SSL modules require the OpenSSL library.
You can either do not enable the modules, or install the OpenSSL library
into the system, or build the OpenSSL library statically from the source
with nginx by using –with-openssl= option.
解决
yum install openssl openssl-devel -y
make &&make install
编译完成
/usr/local/nginx/sbin/nginx -t 检查 nginx 配置文件是否正
确,返回 OK 即正确。
[root@localhost nginx-1.4.2]# /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
启动Nginx
/usr/local/nginx/sbin/nginx
浏览器输入IP地址 出现Nginx的欢迎页面,表示源码编译Nginx成功。
Nginx配置虚拟主机
进入要配置的目录
cd /usr/local/nginx/conf
添加网站信息
vim nginx.conf
server {
listen 80;
server_name www.a.com;
location / {
root html/a;
index index.html index.htm;
}
}
server {
listen 80;
server_name www.b.com;
location / {
root html/b;
index index.html index.htm;
}
}
,有些网站,你得需要用户和密码才能访问的,那么这样的网址需要怎么配置呢,其实也很简单,
第一步
在nginx.conf配置文件里添加相关配置如下
server {
listen 80;
server_name www.b.com;
location / {
root html/b;
index index.html index.htm;
auth_basic "oldboy tranning";
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
}
}
在当前网站的目录下执行
yum -y install httpd
which htpasswd
htpasswd -bc /usr/local/nginx/conf/htpasswd oldboy 123456
chmod 400 /usr/local/nginx/conf/htpasswd
chown nginx /usr/local/nginx/conf/htpasswd
若是有报错
[root@www html]# /etc/init.d/nginx -s reload
nginx: [error] invalid PID number “” in “/usr/local/nginx/logs/nginx.pid”
解决办法是:/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf