To configure a nginx+php Web server

Source: Internet
Author: User
Tags configuration php epoll ibase soap php web server php website tidy install redis

I. BASIC information
    • System (L): CentOS 6.9 #:http://mirrors.sohu.com
    • Web server (N): NGINX 1.14.0 #:http://nginx.org/en/download.html
    • Database Server (M): MySQL 5.6.40 #:https://dev.mysql.com/downloads/mysql
    • PHP-FPM Server (P): php-5.6.8.tar.gz #:http://mirrors.sohu.com/php/
    • Openssl:openssl-1.0.2o.tar.gz #:https://www.openssl.org/source/
Specify a common location for service installation
mkdir /usr/local/servicesSERVICE_PATH=/usr/local/services
Create an account to run the service
useradd -r -M -s /sbin/nologin www
Install the required dependency packages
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
Second, software installation configuration 1, nginx+openssl installation Download decompression 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}
Compile and install 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 cleanup and other configuration after installation is complete
ln -sv ${SERVICE_PATH}/nginx /usr/local/rm -rf ${SERVICE_PATH}/nginx/conf/*.defaultcd ${SERVICE_PATH} ; rm -rf nginx*.tar.gz openssl*.tar.gz
Write Master config file nginx.conf (configuration optimized)
Cat << EOF >/usr/local/nginx/conf/nginx.confuser 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 process configuration, specified as twice times the number of logical CPUs
THREAD=`expr $(grep process /proc/cpuinfo |wc -l) \* 2`sed -i s"/WORKERNUMBER/$THREAD/" ${SERVICE_PATH}/nginx/conf/nginx.conf
2, PHP-FPM installation download and decompression PHP-FPM software
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}
Compiling and installing 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  
If the FPM program has plug-in requirements, such as MONGO or REDIS connection plug-ins, you can install the PHP-related plugin via pecl ${service_path}/php/bin/pecl install MONGO | | Exit${service_path}/php/bin/pecl Install Redis | | Exit configuration cleanup After installation is complete

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

PHP.ini configuration file Write (configuration optimized)
Cat << EOF >${service_path}/php/php.ini [php]engine = Onshort_open_tag = Offasp_tags = Offprecision = 14output_bu ffering = 4096zlib.output_compression = Offimplicit_flush = Offunserialize_callback_func =serialize_precision = 17disable_functions = shell_exec,phpinfo,execdisable_classes =zend.enable_gc = onexpose_php = Offmax_execution_time = 60max_input_time = 60memory_limit = 128merror_reporting = e_waring & errordisplay_errors = Offdisplay_startup_errors = Offlog_errors = Onlog_errors_max_len = 2048ignore_repeated_errors = Offignore_repeated_source = Offreport_memleaks = On Track_errors = Offhtml_errors = Offerror_log =/var/log/php_errors.logvariables_order = "GPCS" Request_order = "GP" REGISTER_ARGC_ARGV = Offauto_globals_jit = Onpost_max_size = 8mauto_prepend_file =auto_append_file =default_mimetype = " text/html "Default_charset =" UTF-8 "doc_root =user_dir =enable_dl = Offcgi.fix_pathinfo=0file_uploads = Onupload_max_ FileSize = 2mmax_file_uploads = 20allow_url_fopen = OffaLlow_url_include = Offdefault_socket_timeout = 60[cli Server]cli_server.color = On[date][filter][iconv][intl][sqlite] [Sqlite3] [Pcre] [Pdo] [pdo_mysql]pdo_mysql.cache_size = 2000pdo_mysql.default_socket=[phar][mail FUNCTION]SMTP = Localhostsmtp_port = 25mail.add_x_header = On[sql]sql.safe_mode = Off[odbc]odbc.allow_persistent = Onodbc.check_persistent = Onodbc.max_ Persistent = -1odbc.max_links = -1ODBC.DEFAULTLRL = 4096odbc.defaultbinmode = 1[interbase]ibase.allow_persistent = 1ibase.max_persistent = -1ibase.max_links = -1ibase.timestampformat = "%y-%m-%d%h:%m:%s" Ibase.dateformat = "%Y-%m-%d" Ibase.timeformat = "%h:%m:%s" [mysql]mysql.allow_local_infile = Onmysql.allow_persistent = Onmysql.cache_size = 2000mysql.max_persistent = -1mysql.max_links = -1mysql.default_port =mysql.default_socket =mysql.default_host = Mysql.default_user =mysql.default_password =mysql.connect_timeout = 60mysql.trace_mode = Off[MySQLi]mysqli.max_ Persistent = -1mysqli.allow_persistent = Onmysqli.max_links = -1mysqli. cache_size = 2000mysqli.default_port = 3306mysqli.default_socket =mysqli.default_host =mysqli.default_user = MYSQLI.DEFAULT_PW =mysqli.reconnect = Off[mysqlnd]mysqlnd.collect_statistics = Onmysqlnd.collect_memory_statistics = Off[oci8][postgresql]pgsql.allow_persistent = Onpgsql.auto_reset_persistent = Offpgsql.max_persistent = -1pgsql.max_ Links = -1pgsql.ignore_notice = 0pgsql.log_notice = 0[sybase-ct]sybct.allow_persistent = Onsybct.max_persistent =- 1sybct.max_links = -1sybct.min_server_severity = 10sybct.min_client_severity = 10[bcmath]bcmath.scale = 0[browscap][ Session]session.save_handler = Filessession.save_path = "/tmp" Session.use_strict_mode = 0session.use_cookies = 1session.use_only_cookies = 1session.name = Phpsessidsession.auto_start = 0session.cookie_lifetime = 0session.cookie_ Path =/session.cookie_domain =session.cookie_httponly =session.serialize_handler = phpsession.gc_probability = 1session.gc_divisor = 1000session.gc_maxlifetime = 1440session.referer_check =session.cAche_limiter = Nocachesession.cache_expire = 180session.use_trans_sid = 0session.hash_function = 0session.hash_bits_ Per_character = 5url_rewriter.tags = "A=href,area=href,frame=src,input=src,form=fakeentry" [MSSQL]mssql.allow_ Persistent = Onmssql.max_persistent = -1mssql.max_links = -1mssql.min_error_severity = 10mssql.min_message_severity = 10mssql.compatibility_mode = Offmssql.secure_connection = Off[assertion][com][mbstring][gd]gd.jpeg_ignore_warning = 0[exif][tidy]tidy.clean_output = off[soap]soap.wsdl_cache_enabled=1soap.wsdl_cache_dir= "/tmp" Soap.wsdl_cache_ttl =86400soap.wsdl_cache_limit = 5[sysvshm][ldap]ldap.max_links = -1[mcrypt][dba][opcache]opcache.enable= 1opcache.enable_cli=0opcache.memory_consumption=128opcache.interned_strings_buffer=8opcache.max_accelerated_ Files=4000opcache.validate_timestamps=1opcache.revalidate_freq=30opcache.fast_shutdown=1opcache.enable_file_ Override=1[curl][openssl]extension_dir= ' ${service_path}/php/lib/php/extensions/'; extension=mongo.so;extension= REdis.soeof 
php-fpm.conf configuration file Write (configuration optimized)
cat << EOF >${SERVICE_PATH}/php/etc/php-fpm.conf[global]pid = /tmp/php-fpm.piderror_log = /var/log/php-fpm-error.loglog_level = warningprocess_control_timeout = 10rlimit_files = 655350events.mechanism = epoll[www]user = wwwgroup = wwwlisten = /dev/shm/php-fpm.socklisten.backlog = 2048listen.owner = wwwlisten.group = wwwlisten.mode = 0660pm = dynamicpm.max_children = 200pm.start_servers = 105pm.min_spare_servers = 10pm.max_spare_servers = 200pm.process_idle_timeout = 10s;pm.max_requests = 1000pm.status_path = /fpmstatusping.path = /pingping.response = pongslowlog = /var/log/php-slow-$pool.logrequest_slowlog_timeout = 10request_terminate_timeout = 0rlimit_files = 655350security.limit_extensions = .phpEOF
Third, based on the above configuration PHP website

mkdir /usr/local/nginx/conf/vhost

cat << EOF > /usr/local/nginx/conf/vhost/erbiao.ex.com.confserver{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

If you run Nginx and php-fpm on the same server, the concurrency amount is not more than 1000, select the UNIX socket, so you can avoid some check operations (routing, etc.), so faster and lighter. For high concurrency, choose a more reliable TCP socket to maintain efficiency with load balancing, kernel optimization, and other operational means

Iv. start the service start Nginx and PHP-FPM
/usr/local/nginx/sbin/nginx/usr/local/php-fpm/sbin/php-fpm
Command other options
nginx├── -s选项,向主进程发送信号|   ├── reload参数,重新加载配置文件|   ├── stop参数,快速停止nginx|   ├── reopen参数,重新打开日志文件|   ├── quit参数,Nginx在退出前完成已经接受的连接请求├── -t选项,检查配置文件是否正确├── -c选项,用于指定特定的配置文件并启动nginx├── -V选项(大写),显示nginx编译选项与版本信息php-fpm├── -t选项,检查配置文件是否正确├── -m选项,显示所有已安装模块├── -i选项,显示PHP详细信息├── -v选项,显示版本信息

To configure a nginx+php Web server

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.