配置一个nginx+php的web服务器

一、基本信息

指定服务安装的通用位置
mkdir /usr/local/services
SERVICE_PATH=/usr/local/services
创建服务运行的账户
useradd -r -M -s /sbin/nologin www
安装所需依赖包
yum -y install pcre pcre-devel gperftools gcc zlib-devel libxml2 libxml2-devel bzip2 bzip2-devel curl curl-devel libjpeg-devel libjpeg libpng-devel libpng freetype freetype-devel libmcrypt libmcrypt-devel openssl-devel

二、软件安装配置

1、NGINX+OPENSSL安装
下载解压NGINX+OPENSSL
NGINX_URL="http://nginx.org/download/nginx-1.14.0.tar.gz"
OPENSSL_URL="https://www.openssl.org/source/openssl-1.1.0h.tar.gz"

wget -P ${SERVICE_PATH} ${NGINX_URL} && tar -zxvf ${SERVICE_PATH}/nginx*.tar.gz -C ${SERVICE_PATH}
wget -P ${SERVICE_PATH} ${OPENSSL_URL} && tar -zxvf ${SERVICE_PATH}/openssl*.gz -C ${SERVICE_PATH}
编译安装NGINX
cd ${SERVICE_PATH}/nginx-*;./configure --prefix=${SERVICE_PATH}/nginx --user=www --group=www --with-http_stub_status_module --with-http_ssl_module --with-http_flv_module --with-pcre --with-http_gzip_static_module --with-openssl=${SERVICE_PATH}/openssl* --with-http_realip_module --with-google_perftools_module --without-select_module --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module --without-poll_module --without-http_autoindex_module --without-http_geo_module --without-http_uwsgi_module --without-http_scgi_module --without-http_memcached_module --with-cc-opt=‘-O2‘ && cd ${SERVICE_PATH}/nginx-*;make && make install
NGINX+OPENSSL安装完成后的清理与其他配置
ln -sv ${SERVICE_PATH}/nginx /usr/local/
rm -rf ${SERVICE_PATH}/nginx/conf/*.default
cd ${SERVICE_PATH} ; rm -rf nginx*.tar.gz openssl*.tar.gz
写入主配置文件nginx.conf(配置已优化)
cat << EOF >/usr/local/nginx/conf/nginx.conf
user www;
worker_processes WORKERNUMBER;
worker_cpu_affinity auto;
worker_rlimit_nofile 655350;

error_log /var/log/nginx_error.log;
pid /tmp/nginx.pid;

google_perftools_profiles /tmp/tcmalloc;

events {
use epoll;
worker_connections 655350;
multi_accept on;
}

http {
charset utf-8;
include mime.types;
default_type text/html;

log_format main ‘"\$remote_addr" - [\$time_local] "\$request" ‘
‘\$status $body_bytes_sent "\$http_referer" ‘
‘"\$http_user_agent" ‘
‘"\$sent_http_server_name \$upstream_response_time" ‘
‘\$request_time ‘
‘\$args‘;

sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 120;
client_body_buffer_size 512k;
client_header_buffer_size 64k;
large_client_header_buffers 4 32k;
client_max_body_size 300M;
client_header_timeout 15s;
client_body_timeout 50s;
open_file_cache max=102400 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 1;
server_names_hash_max_size 2048;
server_names_hash_bucket_size 256;
server_tokens off;
gzip on;
gzip_proxied any;
gzip_min_length 1024;
gzip_buffers 4 8k;
gzip_comp_level 9;
gzip_disable "MSIE [1-6]\.";
gzip_types application/json test/html text/plain text/css application/font-woff application/pdf application/octet-stream application/x-javascript application/javascript application/xml text/javascript;
fastcgi_cache_path /dev/shm/ levels=1:2 keys_zone=fastcgicache:512m inactive=10m max_size=3g;
fastcgi_cache_lock on;
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
fastcgi_send_timeout 300;
fastcgi_connect_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 256k;
fastcgi_buffers 4 128k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;

include vhost/*.conf;
}
EOF
NGINX worker进程数配置,指定为逻辑CPU数量的2倍
THREAD=`expr $(grep process /proc/cpuinfo |wc -l) \* 2`
sed -i s"/WORKERNUMBER/$THREAD/" ${SERVICE_PATH}/nginx/conf/nginx.conf
2、PHP-FPM安装
下载并解压PHP-FPM软件
FPM_URL="http://mirrors.sohu.com/php/php-5.6.8.tar.gz"
wget -P ${SERVICE_PATH} ${FPM_URL} && tar -zxvf ${SERVICE_PATH}/php*.tar.gz -C ${SERVICE_PATH}
编译安装PHP-FPM
cd ${SERVICE_PATH}/php-*;./configure --prefix=${SERVICE_PATH}/php --with-gd --with-mcrypt --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --enable-maintainer-zts --enable-ftp --enable-zip --with-bz2 -with-iconv-dir --with-freetype-dir --with-jpeg-dir --with-png-dir --with-config-file-path=${SERVICE_PATH}/php --enable-mbstring --enable-fpm --with-fpm-user=www --with-fpm-group=www --disable-debug --enable-opcache --enable-soap --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --with-pear --with-gettext --disable-fileinfo && cd ${SERVICE_PATH}/php-*;make && make install
若FPM程序有插件需求,如mongo或redis连接插件,则可通过pecl安装php相关插件
${SERVICE_PATH}/php/bin/pecl install mongo || exit
${SERVICE_PATH}/php/bin/pecl install redis || exit
安装完成后的配置清理

ln -sv ${SERVICE_PATH}/php /usr/local/

php.ini配置文件写入(配置已优化)
cat << EOF >${SERVICE_PATH}/php/php.ini
[PHP]
engine = On
short_open_tag = Off
asp_tags = Off
precision = 14
output_buffering = 4096
zlib.output_compression = Off
implicit_flush = Off
unserialize_callback_func =
serialize_precision = 17
disable_functions = shell_exec,phpinfo,exec
disable_classes =
zend.enable_gc = On
expose_php = Off
max_execution_time = 60
max_input_time = 60
memory_limit = 128M
error_reporting = E_WARING & ERROR
display_errors = Off
display_startup_errors = Off
log_errors = On
log_errors_max_len = 2048
ignore_repeated_errors = Off
ignore_repeated_source = Off
report_memleaks = On
track_errors = Off
html_errors = Off
error_log = /var/log/php_errors.log
variables_order = "GPCS"
request_order = "GP"
register_argc_argv = Off
auto_globals_jit = On
post_max_size = 8M
auto_prepend_file =
auto_append_file =
default_mimetype = "text/html"
default_charset = "UTF-8"
doc_root =
user_dir =
enable_dl = Off
cgi.fix_pathinfo=0
file_uploads = On
upload_max_filesize = 2M
max_file_uploads = 20
allow_url_fopen = Off
allow_url_include = Off
default_socket_timeout = 60
[CLI Server]
cli_server.color = On
[Date]
[filter]
[iconv]
[intl]
[sqlite]
[sqlite3]
[Pcre]
[Pdo]
[Pdo_mysql]
pdo_mysql.cache_size = 2000
pdo_mysql.default_socket=
[Phar]
[mail function]
SMTP = localhost
smtp_port = 25
mail.add_x_header = On
[SQL]
sql.safe_mode = Off
[ODBC]
odbc.allow_persistent = On
odbc.check_persistent = On
odbc.max_persistent = -1
odbc.max_links = -1
odbc.defaultlrl = 4096
odbc.defaultbinmode = 1
[Interbase]
ibase.allow_persistent = 1
ibase.max_persistent = -1
ibase.max_links = -1
ibase.timestampformat = "%Y-%m-%d %H:%M:%S"
ibase.dateformat = "%Y-%m-%d"
ibase.timeformat = "%H:%M:%S"
[MySQL]
mysql.allow_local_infile = On
mysql.allow_persistent = On
mysql.cache_size = 2000
mysql.max_persistent = -1
mysql.max_links = -1
mysql.default_port =
mysql.default_socket =
mysql.default_host =
mysql.default_user =
mysql.default_password =
mysql.connect_timeout = 60
mysql.trace_mode = Off
[MySQLi]
mysqli.max_persistent = -1
mysqli.allow_persistent = On
mysqli.max_links = -1
mysqli.cache_size = 2000
mysqli.default_port = 3306
mysqli.default_socket =
mysqli.default_host =
mysqli.default_user =
mysqli.default_pw =
mysqli.reconnect = Off
[mysqlnd]
mysqlnd.collect_statistics = On
mysqlnd.collect_memory_statistics = Off
[OCI8]
[PostgreSQL]
pgsql.allow_persistent = On
pgsql.auto_reset_persistent = Off
pgsql.max_persistent = -1
pgsql.max_links = -1
pgsql.ignore_notice = 0
pgsql.log_notice = 0
[Sybase-CT]
sybct.allow_persistent = On
sybct.max_persistent = -1
sybct.max_links = -1
sybct.min_server_severity = 10
sybct.min_client_severity = 10
[bcmath]
bcmath.scale = 0
[browscap]
[Session]
session.save_handler = files
session.save_path = "/tmp"
session.use_strict_mode = 0
session.use_cookies = 1
session.use_only_cookies = 1
session.name = PHPSESSID
session.auto_start = 0
session.cookie_lifetime = 0
session.cookie_path = /
session.cookie_domain =
session.cookie_httponly =
session.serialize_handler = php
session.gc_probability = 1
session.gc_divisor = 1000
session.gc_maxlifetime = 1440
session.referer_check =
session.cache_limiter = nocache
session.cache_expire = 180
session.use_trans_sid = 0
session.hash_function = 0
session.hash_bits_per_character = 5
url_rewriter.tags = "a=href,area=href,frame=src,input=src,form=fakeentry"
[MSSQL]
mssql.allow_persistent = On
mssql.max_persistent = -1
mssql.max_links = -1
mssql.min_error_severity = 10
mssql.min_message_severity = 10
mssql.compatibility_mode = Off
mssql.secure_connection = Off
[Assertion]
[COM]
[mbstring]
[gd]
gd.jpeg_ignore_warning = 0
[exif]
[Tidy]
tidy.clean_output = Off
[soap]
soap.wsdl_cache_enabled=1
soap.wsdl_cache_dir="/tmp"
soap.wsdl_cache_ttl=86400
soap.wsdl_cache_limit = 5
[sysvshm]
[ldap]
ldap.max_links = -1
[mcrypt]
[dba]
[opcache]
opcache.enable=1
opcache.enable_cli=0
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.validate_timestamps=1
opcache.revalidate_freq=30
opcache.fast_shutdown=1
opcache.enable_file_override=1
[curl]
[openssl]
extension_dir=‘${SERVICE_PATH}/php/lib/php/extensions/‘
;extension=mongo.so
;extension=redis.so
EOF
php-fpm.conf配置文件写入(配置已优化)
cat << EOF >${SERVICE_PATH}/php/etc/php-fpm.conf
[global]
pid = /tmp/php-fpm.pid
error_log = /var/log/php-fpm-error.log
log_level = warning
process_control_timeout = 10
rlimit_files = 655350
events.mechanism = epoll
[www]
user = www
group = www
listen = /dev/shm/php-fpm.sock
listen.backlog = 2048
listen.owner = www
listen.group = www
listen.mode = 0660
pm = dynamic
pm.max_children = 200
pm.start_servers = 105
pm.min_spare_servers = 10
pm.max_spare_servers = 200
pm.process_idle_timeout = 10s;
pm.max_requests = 1000
pm.status_path = /fpmstatus
ping.path = /ping
ping.response = pong
slowlog = /var/log/php-slow-$pool.log
request_slowlog_timeout = 10
request_terminate_timeout = 0
rlimit_files = 655350
security.limit_extensions = .php
EOF

三、基于以上配置PHP网站

mkdir /usr/local/nginx/conf/vhost

cat << EOF > /usr/local/nginx/conf/vhost/erbiao.ex.com.conf
server
{
listen 80 backlog=1024;
server_name erbiao.ex.com;
index index.php index.html ;
root /www/web/;
access_log off;
add_header Server-Name WEBerbiaoEX;

location ~ \.php {
fastcgi_pass unix:/dev/shm/php-fpm.sock;
fastcgi_index index.php;
include fastcgi.conf;
set \$real_script_name \$fastcgi_script_name;
if (\$fastcgi_script_name ~ "^(.+?\.php)(/.+)\$") {
set \$real_script_name \$1;
set \$path_info \$2;
}
fastcgi_param SCRIPT_FILENAME \$document_root\$real_script_name;
fastcgi_param SCRIPT_NAME \$real_script_name;
fastcgi_param PATH_INFO \$fastcgi_path_info;

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}

location ~ .*\.(js|css)?$
{
expires 12h;
}

}

}
EOF

若在同一服务器运行nginx和php-fpm,并发量不超过1000,选择unix socket,如此可避免一些检查操作(路由等),因此更快,更轻。若是高并发业务,则选择使用更可靠的tcp socket,以负载均衡、内核优化等运维手段维持效率

四、启动服务

启动nginx和php-fpm
/usr/local/nginx/sbin/nginx
/usr/local/php-fpm/sbin/php-fpm
命令其他选项
nginx
├── -s选项,向主进程发送信号
|   ├── reload参数,重新加载配置文件
|   ├── stop参数,快速停止nginx
|   ├── reopen参数,重新打开日志文件
|   ├── quit参数,Nginx在退出前完成已经接受的连接请求
├── -t选项,检查配置文件是否正确
├── -c选项,用于指定特定的配置文件并启动nginx
├── -V选项(大写),显示nginx编译选项与版本信息

php-fpm
├── -t选项,检查配置文件是否正确
├── -m选项,显示所有已安装模块
├── -i选项,显示PHP详细信息
├── -v选项,显示版本信息

原文地址:https://www.cnblogs.com/erbiao/p/9237851.html

时间: 2024-10-04 11:38:36

配置一个nginx+php的web服务器的相关文章

SPWebServer:一个基于 SPServer 的 web 服务器框架

SPWebServer:一个基于 SPServer 的 web 服务器框架 博客分类: OpenSource项目 应用服务器框架Web网络应用多线程 看到这个题目,估计很多人会问:为什么要再实现一个 web 服务器? 这里有几个原因: 1.这是一个 web 服务器框架,不是一个完整的 web 服务器.也就是说 SPWebServer 提供的是一套 API 和类库,可以方便地集成到现有的应用程序中.可以称 SPWebServer 为 embedded web server . 2.有些时候,我们需

Nginx搭建部署Web服务器并与NFS结合搭建负载均衡服务器

Nginx搭建部署Web服务器并与NFS结合搭建负载均衡服务器 一.搭建NginxWeb服务器     此种方式是用yum安装Nginx,为保证安装成功需在安装之前提前安装epel扩展源.     用yum安装Nginx     关闭系统防火墙           查看Nginx配置文件          安装完成后启动Nginx并查看其运行状态          然后在客户端浏览器里面输入Nginx服务器的IP地址即可验证Nginx工作正常      二.搭建NFS服务器     NFS:网络

脚本实现一键部署Nginx软件(Web服务器):

案例1:编写一键部署软件脚本 案例2:启动脚本 案例3:编写监控脚本 案例4:编写安全检测脚本 案例5:编写进度显示脚本 1 案例1:编写一键部署软件脚本1.1 问题 本案例要求编写脚本实现一键部署Nginx软件(Web服务器): 一键源码安装Nginx软件 脚本自动安装相关软件的依赖包 脚本自动判断yum是否可用 1.2 步骤 实现此案例需要按照如下步骤进行. 步骤一:准备工作 1)判断yum源是否可用 通过yum repolist查看软件包数量是否大于0: [[email protected

一个简单的Java web服务器实现

一个简单的Java web服务器实现,比较简单,基于java.net.Socket和java.net.ServerSocket实现: 程序执行步骤 创建一个ServerSocket对象: 调用ServerSocket对象的accept方法,等待连接,连接成功会返回一个Socket对象,否则一直阻塞等待: 从Socket对象中获取InputStream和OutputStream字节流,这两个流分别对应request请求和response响应: 处理请求:读取InputStream字节流信息,转成字

Nginx做为web服务器的网站

Nginx 超越Apache的高性能和稳定性,使得国内使用 Nginx 作为 Web 服务器的网站也越来越多,其中有新浪.网易.腾讯等门户网站,六间房.酷6等视频分享网站,Discuz!.水木社区等知名论坛,豆瓣.YUPOO等新兴Web 2.0网站. Nginx 在国内的应用正在不断发展壮大! 一.使用 Nginx 运行 PHP(FastCGI) 程序的网站: 1.六间房视频(http://www.6.cn/):nginx/0.6.14 + PHP [[email protected] ~]#

ubuntu 12.04 安装 nginx+php+mysql web服务器

Nginx 是一个轻量级,以占用系统资源少,运行效率而成为web服务器的后起之秀,国内现在很多大型网站都以使用nginx,包括腾讯.新浪等大型信息网站,还有淘宝网站使用的是nginx二次开发的web服务器. 直接php+mysql这是Linux web服务器的黄金组合.今天我们一来学如何在ubuntu 12.04 中架设lnmp. 1. 使用官方PPA安装 Nginx 最新版本,使用以下命令:sudo add-apt-repository ppa:nginx/stablesudo apt-get

[转] ubuntu 12.04 安装 nginx+php+mysql web服务器

Nginx 是一个轻量级,以占用系统资源少,运行效率而成为web服务器的后起之秀,国内现在很多大型网站都以使用nginx,包括腾讯.新浪等大型信息网站,还有淘宝网站使用的是nginx二次开发的web服务器. 直接php+mysql这是Linux web服务器的黄金组合.今天我们一来学如何在ubuntu 12.04 中架设lnmp. 1. 使用官方PPA安装 Nginx 最新版本,使用以下命令:sudo add-apt-repository ppa:nginx/stablesudo apt-get

Nodejs 入门秘籍,搭一个最简单的web服务器。(菜鸟的你也能秒懂)

心血来潮,总结了一个Nodejs入门秘籍.看了你就想学NodeJs了. 一.简单介绍nodejs 初学nodejs  ,直接上简单实例.先简单说明一下:nodejs ,nodejs相当于服务器端的 可以轻易搭建一个服务器端. nodejs是用C++开发的一种运行于服务器端的语言,可以写网站后台程序,可以做服务端应用开发,语法和JAVASCRIPT一样,会JS,就是会NodeJS,区别于,普通JS是脚本运行客户端,而NODEJS中的JS是运行于服务器端,NODEJS的作用相当PHP,ASP等语言.

Tomcat通过配置一个虚拟路径管理web工程

关于虚拟路径.学问javaweb训练课程,如今,鉴于这种情况下老师. 当我们的项目,当在不同的文件夹项目.我们如何使用tomcat去管理web工程. 教师提出的解决方案是 使用虚拟路径方式,并按照实施例,以下面的方式tomcat进行配置.使得 tomcat能够管理不同文件夹上的项目. 我的问题:项目不是转换成war包公布到tomcat上吗,然后放在了webapps文件夹以下吗? 或者直接把项目放置webapps以下. 1.为什么有可能出现 管理不同文件夹下的web项目呢? 2.上面两种方式,处理