YUM安装
yum install nginx
service nginx start
chkconfig nginx on
2.mysql
yum install mysql mysql-server mysql-devel
service mysqld start
chkconfig mysqld on
3.PHP
yum install php
yum install php-mysql php-gd libjpeg* php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-mcrypt php-bcmath php-mhash libmcrypt libmcrypt-devel php-fpm #安装PHP组件,使PHP支持 MySQL、PHP支持FastCGI模式
service php-fpm start
chkconfig php-fpm on
4.配置
修改 /etc/php-fpm.d/www.conf
user = nginx
group = nginx
默认nginx目录 /usr/share/nginx/html
chown -R root.nginx /var/lib/php/session/
源码安装
yum install gcc gcc 编译器
yum install -y pcre pcre-devel Rewrite和HTTP模块会用到PCRE
yum install -y zlib zlib-devel Gzip用到zlib
yum install -y openssl openssl-devel SSL 用
下载nginx wget http://101.96.8.165/nginx.org/download/nginx-1.11.6.tar.gz
解压 tar -zxvf nginx-1.11.6.tar.gz
./configure 编译时注意会提示你 文件的默认安装位置
make
make install
nginx -h 查看帮助
文件位置 /usr/local/nginx
启动文件 /usr/loca/nginx/sbin/nginx
安装php和 php-fpm
yum install php php-mysql php-common php-gd php-mbstring php-mcrypt php-devel php-xml php-fpm
/etc/init.d/php-fpm start 启动php-fpm
chkconfig php-fpm on 开启启动
错误提示
错误A:
Starting nginx: nginx: [emerg] socket() [::]:80 failed (97: Address family not supported by protocol)
单网站直接在/etc/nginx/conf.d/default.conf 编辑即可
多网站配置
/etc/nginx/conf.d/default.conf 中有一句 include /etc/nginx/default.d/*.conf;
在/etc/nginx/default.d/目录 新建 linux.conf
server {
listen 80;
server_name linux.nongpingou.cn;
access_log logs/linux-access.log main; #自己创建 nginx用户有读写权限
error_log logs/linux-error.log; #错误日志
#autoindex on; //开启目录浏览 默认关闭
root /usr/share/nginx/linux_web; #主目录
index index.php index.html index.htm; #默认主页
}
error_page 500 502 503 504 /50x.html; #错误提示
location = /50x.html {
root /usr/share/nginx/linux;
}
location ~* ^/pic/.*.(php|php5)$ { #禁止单目录
deny all;
}
location ~* ^/(upload|images)/.*.(php|php5)$ { #禁止多目录
deny all;
}
location ~ .php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/linux/$fastcgi_script_name;
include fastcgi_params;
fastcgi_param PHP_ADMIN_VALUE "open_basedir=/usr/share/nginx/linux/:/tmp/"; #最大访问目录
}
location ~ /.ht {
deny all;
}
location /123 { #禁止访问某个目录
deny all;
return 403;
}
}
禁止目录执行php的主意事项
1、以上的配置文件代码需要放到 location ~ .php{...}上面,如果放到下面是无效的
2、attachments需要写相对路径,不能写绝对路径
3、不要忘记重启nginx呀,service nginx restart
隐藏nginx版本
/etc/nginx/nginx.conf
http{ } 加入 server_tokens off;
nginx.conf
# For more information on configuration, see:
有关配置的更多信息,请参见:
# * Official English Documentation: http://nginx.org/en/docs/
# *官方英文文档:http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
#俄罗斯官方文档:http://nginx.org/ru/docs/
user nginx;
#用户组
worker_processes auto;
##nginx进程数,建议设置与cpu核心数一致
error_log /var/log/nginx/error.log;
##全局错误日志定义类型
pid /var/run/nginx.pid;
##进程文件
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
##负载动态模块看 /usr/share/nginx/README
include /usr/share/nginx/modules/*.conf;
##动态加载模块
events {
worker_connections 1024;
##单个进程的最大连接数
}
设置http服务器
http {
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 /var/log/nginx/access.log main;
##访问日志
sendfile on;
##sendfile参数用于开启文件的高效传输模式。同时将tcp_nopush和tcp_nodelay两个指令设置为on,可防止网络及磁盘I/O阻塞,提升Nginx工作效率。
##开启目录列表访问,合适下载服务器,默认关闭
tcp_nopush on;
##防止网络阻塞 建议打开
tcp_nodelay on;
##防止网络阻塞 建议打开
keepalive_timeout 65;
##长链接超时时间,单位是秒,为0,无超时
types_hash_max_size 2048;
server_tokens off;
##隐藏Nginx的版本号
include /etc/nginx/mime.types;
##文件扩展名与文件类型映射表
default_type application/octet-stream;
##默认文件类型
# Load modular configuration files from the /etc/nginx/conf.d directory.
#负载模块配置文件从/ etc / Nginx / conf.d目录。
# See http://nginx.org/en/docs/ngx_core_module.html#include
#看http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
#了解更多信息。
include /etc/nginx/conf.d/*.conf;
##引入外置配置文件
}