安装Nginx程序,一般有三种安装方式:
1.直接网络安装;
2.下载rpm包,上传至服务器进行安装;
3.通过原代码编译安装;
一、安装基础支持套件
yum -y install gcc
yum -y install openssl
yum -y install openssl-devel
yum -y install pcre
yum -y install pcre-devel
二、安装Nginx服务端
wget http://nginx.org/download/nginx-1.5.8.tar.gz
tar xzvf nginx-1.5.8.tar.gz
cd nginx-1.5.8
./cofnigure --prefix=/usr/local/nginx
make
make install
启动/停止/状态:
/usr/local/nginx/sbin/nginx
killall -9 nginx
ps -rf | grep nginx
/usr/local/nginx/sbin/nginx -t //测试nginx配置;
echo "/usr/local/nginx/sbin/nginx">>/etc/rc.local //添加到开机自启动;
安装启动好后,测试访问一下:http://ip地址
如果显示出来:Welcome to nginx! 表示已经安装好了;
三、进阶配置
Nginx目前还不能直接支持php,必须先借助于fastcgi来驱动php。现在fastcgi较好的办法有2种,一个是spawn-fcgi,另外一个就是php-fpm。一般来说可能php-fpm更强大一点,php5.3.3版本之后,php-fpm已经被PHP官方收录了,在./configure的时候带 --enable-fpm参数即可开启PHP-FP ,这里我介绍第一种;
3.1安装 spawn-fcgi 工具:
wget http://www.lighttpd.net/download/spawn-fcgi-1.6.3.tar.gz
tar xzvf spawn-fcgi-1.6.3.tar.gz
cd spawn-fcgi-1.6.3
./configure
make
make install
3.2安装配置PHP与Mysql
yum -y install php
yum -y install mysql
yum -y install php-gd
yum -y install php-mysql
------------------------------
vim /etc/php.ini
找到
request_order = "GP"
更改为
request_order = "CGP"
3.3启动FastCGI:
spawn-fcgi -a 127.0.0.1 -p 9000 -C 10 -f /usr/bin/php-cgi
3.4配置Nginx主配置文件
vim /usr/local/nginx/conf/nginx.conf
user root;
worker_processes 2;
events {
worker_connections 2048;
}
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;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root /var/www/html;
index index.php index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ \.php$ {
root /var/www/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
include fastcgi_params;
}
四、nginx禁止未绑定域名/IP访问
编辑nginx.conf主配置文件,增加如下内容即可:
server {
listen 80 default_server;
server_name _;
return 404;
}
注:server_name 如“ - “ 和” !@# “也可同样使用。
killall -9 nginx
killall -9 php-cgi
spawn-fcgi -a 127.0.0.1 -p 9000 -C 10 -f /usr/bin/php-cgi
/usr/local/nginx/sbin/nginx
五、启用SSL功能
#yum -y install openssl openssl-devel
#cd /usr/local/nginx/conf
#openssl genrsa -des3 -out server.key 1024
#openssl req -new -key server.key -out server.csr
#openssl rsa -in server.key -out server_nopwd.key
#openssl x509 -req -days 365 -in server.csr -signkey server_nopwd.key -out server.crt
#证书生成完毕;
#接下来在nginx.conf主配置文件里启用ssl相关配置即可;
#前提是http_ssl_module已经安装;
#查看nginx是否已经安装了http_ssl_module模块,命令是:/usr/local/nginx/sbin/nginx -V
六、YUM方式安装Nginx
rpm -ivh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
yum -y install gcc vim lrzsz wget nginx php php-fpm php-mysql
cd /etc/nginx/conf.d/
编辑 default.conf 文件,开启PHP功能:
location ~ \.php$ { root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
include fastcgi_params;
}
/etc/init.d/php-fpm start
/etc/init.d/nginx start
touch index.php
cat <<eof >> index.php
<?php
phpinfo ();
?>
eof
iptables -I INPUT -m state --state NEW -m tcp -p tcp --dprot 80 -j ACCEPT
七、安装扩展模块
由于之前没有安装GeoIP模块,现在需要启用该功能,操作方法如下:
wget http://geolite.maxmind.com/download/geoip/api/c/GeoIP.tar.gz
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
tar xzvf GeoIP.tar.gz
cd GeoIP-1.4.8
./configure --prefix=/usr/local/geoip
make;make install
wget http://nginx.org/download/nginx-1.9.0.tar.gz
tar xzvf nginx-1.9.0.tar.gz
#原来有安装过的,上面的步骤可以忽略;
cd nginx-1.9.0
/usr/local/nginx/sbin/nginx -V
先获取原来编译的参数,再加上我们要安装的模块;
./configure --prefix=/usr/local/nginx --with-debug --with-http_geoip_module --with-http_stub_status_module --with-stream --with-http_ssl_module
make #注意不要再make install;
/etc/init.d/nginx stop
cp ./objs/nginx /usr/local/nginx/sbin/
echo "/usr/local/lib" >> /etc/ld.so.conf
ldconfig
/etc/init.d/nginx start
注:在配置(./configure)的时候,会报错,提示找不到GeoIP的lib文件,这是由于我们在前面安装GeoIP的时候,指定的自定义的路径导致,如果没有指定路径的话,一般是没有什么问题;由于指定了,所以需要做一下软链接:
ln -s /usr/local/geoip/lib/* /usr/local/lib/
ln -s /usr/local/geoip/include/* /usr/local/include/
到这里,我们的扩展模块就安装好了;最后把GeoIP数据文件解压出来,放到相应的位置,最后在nginx的http模块下增加类似如下内容即可:
geoip_country /usr/local/nginx/conf/GeoIP.dat;
geoip_city /usr/local/nginx/conf/GeoLiteCity.dat;
参考 http://www.shangxueba.com/jingyan/123187.html
本文出自 “方寸小山” 博客,请务必保留此出处http://63638790.blog.51cto.com/513514/1350021