linux centos7.3安装lnmp,nginx-1.11.12 ,php7.0.2 ,

#更新源

yum -y update

 #添加用户和组

adduser www
groupadd www
usermod -G www www

 

#初始化目录 

mkdir -p /data/app/php
mkdir -p /data/app/nginx
mkdir -p /data/www
mkdir -p /data/download
mkdir -p /data/logs
chown www.www /data/logs
mkdir -p /data/logs/www
chown www.www /data/logs/www
mkdir -p /data/logs/nginx
chown www.www /data/logs/nginx
mkdir /data/logs/php
chown www.www /data/logs/php

touch /data/logs/php/php-fpm.err
chown www.www /data/logs/php/php-fpm.err

touch /data/logs/php/phpslow.log
chown www.www /data/logs/php/phpslow.log
touch /data/logs/php/phpslow2.log
chown www.www /data/logs/php/phpslow2.log
touch /data/logs/php/phpslow3.log
chown www.www /data/logs/php/phpslow3.log

mkdir -p /data/phptmp
chown www.www /data/phptmp

touch /tmp/php-cgi.sock
chown www.www /tmp/php-cgi.sock

touch /tmp/php-cgi2.sock
chown www.www /tmp/php-cgi2.sock

touch /tmp/php-cgi3.sock
chown www.www /tmp/php-cgi3.sock

mkdir /data/logs/rsync

 

#安装依赖

yum install -y gcc gcc-c++ make libtool zlib zlib-devel openssl openssl-devel pcre pcre-devel

 

#####################################
#编译安装nginx
#####################################
#下载Nginx

cd /data/download/
wget http://nginx.org/download/nginx-1.11.12.tar.gz

 

# 解压

tar -zxvf nginx-1.11.12.tar.gz
cd nginx-1.11.12

 

#编译配置

./configure \
--prefix=/data/app/nginx \
--user=www \
--group=www \
--with-pcre \
--with-http_v2_module \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-http_auth_request_module \
--with-mail \
--with-mail_ssl_module \
--with-file-aio \
--with-http_v2_module \
--with-threads \
--with-stream \
--with-stream_ssl_module

#--prefix=/usr/local/nginx-1.12.0 # 定义安装路径,不写时默认为/usr/local/nginx

#--sbin-path= # 定义应用程序存放路径,不写时默认为<prefix>/sbin/nginx
#--conf-path= # 定义配置文件路径,不写时默认为<prefix>/conf/nginx.conf
#--error-log-path=/var/log/nginx/error.log # 在配置文件中没有指定error log时的错误日志路径,不写时默认为<prefix>/logs/error.log
#--http-log-path=/var/log/nginx/access.log # 在配置文件中没有指定access log时的访问日志路径, 不写时默认为<prefix>/logs/access.log
#--pid-path=/var/run/nginx/nginx.pid # pid文件路径,没指定时默认为<prefix>/logs/nginx.pid
#--lock-path=/var/lock/subsys/nginx # 锁文件路径
#--user=nginx # 在配置文件中没有指定user指定时,worker进程的运行身份,不写时默认为nobody
#--group=nginx # 在配置文件中没有指定user(不是group,配置文件中没有group指令)指定时,worker进程的运行组

#--with-select_module # 启用select方法模型,当找不到epoll时自动启用select
#--without-select_module
#--with-poll_module # 启用poll方法模型,当找不到epoll时自动启用poll
#--without-poll_module

#--with-http_ssl_module # 启用ssl功能
#--with-http_flv_module # 启用flv视频流功能
#--with-http_stub_status_module # 启用nginx状态监控功能,在启动后在浏览器使用root/status显示状态信息
#--with-http_gzip_static_module # 启用gzip压缩功能压缩web服务器响应客户端的响应报文
#--http-client-body-temp-path=/var/tmp/nginx/client # 定义客户端请求报文主体的临时文件存放路径,不写为<prefix>/client_body_temp
#--http-proxy-temp-path=/var/tmp/nginx/proxy # 定义从代理服务器收到的临时文件存放路径,不写为<prefix>/proxy_temp
#--http-fastcgi-temp-path=/var/tmp/nginx/fcgi # 定义从fastcgi服务器收到的临时文件存放路径,不写为<prefix>/fastcgi_temp
#--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi # 定义从uwsgi服务器收到的临时文件存放路径,不写为<prefix>/uwsgi_temp
#--http-scgi-temp-path=/var/tmp/nginx/scgi # 定义从scgi服务器收到的临时文件存放路径,不写为<prefix>/scgi_temp
#--with-pcre # 设置pcre库的路径,yum安装的pcre-devel可以不写路径
#--with-threads # 设置nginx支持多线程

#编译安装

make && make install

 

#配置nginx 文件

cp /data/app/nginx/conf/nginx.conf /data/app/nginx/conf/nginx.conf.bak

  cat > /data/app/nginx/conf/nginx.conf << EOF

user  www www;
worker_processes 1;

error_log  /data/logs/nginx/nginx_error.log  warn;
pid        /var/run/nginx.pid;

worker_rlimit_nofile 65535;

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

http {
    include       mime.types;
    default_type  application/octet-stream;

    charset utf8;

    server_tokens off;
    server_name_in_redirect off;

    server_names_hash_bucket_size 128;
    client_header_buffer_size 16k;
    large_client_header_buffers 4 16k;

    client_max_body_size 50m;
    client_body_buffer_size 128K;

    sendfile  on;
    tcp_nopush on;
    keepalive_timeout 60;

    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    fastcgi_buffer_size 128k;
    fastcgi_buffers 32 64k;
    fastcgi_busy_buffers_size 128k;
    fastcgi_temp_file_write_size 156k;

    gzip on;
    gzip_min_length  1k;
    gzip_buffers     4 16k;
    gzip_http_version 1.1;
    gzip_comp_level 6;
    gzip_types     text/plain application/javascript application/x-javascript text/javascript text/css application/xml application/xml+rss;

    log_format snlog '\$remote_addr | \$remote_user | \$time_local | \$request | \$status | '
            '\$bytes_sent | \$body_bytes_sent | \$host | \$http_referer | \$http_user_agent | '
            '\$upstream_addr | \$gzip_ratio | \$http_x_forwarded_for | \$request_time | \$upstream_response_time';

    access_log off;

    open_file_cache max=65535 inactive=20s;
    open_file_cache_valid    30s;
    open_file_cache_min_uses 2;
    open_file_cache_errors   on;

    proxy_connect_timeout      300;
    proxy_send_timeout         300;
    proxy_read_timeout         300;

    proxy_buffering on;
    proxy_buffer_size          32k;
    proxy_buffers              128 32k;
    proxy_busy_buffers_size    256k;
    proxy_temp_file_write_size 256k;
 
    upstream unix__tmp_php_cgi_sock {
        server unix:/tmp/php-cgi.sock weight=100 max_fails=5 fail_timeout=30;
        server unix:/tmp/php-cgi2.sock weight=100 max_fails=5 fail_timeout=30;
        server unix:/tmp/php-cgi3.sock weight=100 max_fails=5 fail_timeout=30;
    }


    # 禁止未授权访问
    server {
        listen 80 default_server;
        server_name _;

        location / {
            return 444;
        }
        access_log  off;
    }

    include vhost/*.conf;
}
EOF

 

#配置多个站点

mkdir -p /data/app/nginx/conf/vhost  

 

cat > /data/app/nginx/conf/vhost/default.conf << EOF
server {
        listen  80;
        server_name www.bheng.top m.bheng.top;
        index index.html index.htm index.php;
        root  /data/www/xiaoshuo;

        location / {
           
                if (!-e $request_filename) {
                        rewrite ^/(.*)$ /index.php?__tp_s=$1 last;
                }
        }

        location ~ \.php$
        {
                index index.php;
                try_files $uri =404;
                fastcgi_pass    unix__tmp_php_cgi_sock;
                include         fastcgi_params;
                fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_param   SCRIPT_NAME $fastcgi_script_name;
        }

        access_log  /data/logs/www/xiaoshuo.log  snlog;
}
EOF

 


#这个先看下nginx是否配置正确

/data/app/nginx/sbin/nginx -t 

#启动 

/data/app/nginx/sbin/nginx 

 

#重启

/data/app/nginx/sbin/nginx -s reload 

 

#关闭80端口程序

fuser -k 80/tcp

 

#############################################################
#安装PHP
#php版本下载地址 https://www.php.net/releases/
#############################################################


#安装依赖包

yum install -y libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel \
freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel

 

#下载php源码包

cd /data/download/
wget https://www.php.net/distributions/php-7.0.20.tar.gz 
tar -zxvf php-7.0.20.tar.gz
cd php-7.0.20

 

#编译配置 直接看见 Thank you for using PHP。如果报错,基本就是相关依赖未安装完全

./configure --prefix=/data/app/php --enable-fpm --with-fpm-user=www --with-fpm-group=www \
--disable-ipv6 --with-openssl --with-pcre-regex --with-zlib --enable-bcmath --with-curl --enable-exif \
--with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --enable-gd-native-ttf --with-gettext \
--with-mhash --enable-mbstring --with-mcrypt --with-mysqli --enable-opcache \
--enable-pcntl --with-pdo-mysql --enable-sockets --with-iconv-dir --with-libxml-dir --enable-zip \
--enable-mysqlnd --with-mysql

 

#编译安装

make && make install

 

#添加 PHP 命令到环境变量

vim /etc/profile

#在文件末尾加上如下两行代码

PATH=$PATH:/data/app/php/bin
export PATH

 

# 立即生效

source /etc/profile

 

#查看PHP版本

php -v
> PHP 7.0.20 (cli) (built: Jul 4 2017 14:39:02) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies

 

#配置php-fpm

cp php.ini-production /etc/php.ini
cp /data/app/php/etc/php-fpm.conf.default /data/app/php/etc/php-fpm.conf
cp /data/app/php/etc/php-fpm.d/www.conf.default /data/app/php/etc/php-fpm.d/www.conf
cp /data/download/php-7.0.20/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod u+x /etc/init.d/php-fpm
chown -R www.www /data/app/php/var

 

cat > /data/app/php/etc/php-fpm.conf << EOF
[global]
pid = /data/app/php/var/run/php-fpm.pid
error_log = /data/logs/php/php-fpm.err
log_level = notice

[www]
listen = /tmp/php-cgi.sock
listen.backlog = 8192
listen.allowed_clients = 127.0.0.1
listen.owner = www
listen.group = www
listen.mode = 0666
user = www
group = www
pm = static
pm.max_children = 5
pm.start_servers = 20
pm.min_spare_servers = 10
pm.max_spare_servers = 40
request_terminate_timeout = 300
request_slowlog_timeout = 10
slowlog = /data/logs/php/phpslow.log

[www2]
listen = /tmp/php-cgi2.sock
listen.backlog = 8192
listen.allowed_clients = 127.0.0.1
listen.owner = www
listen.group = www
listen.mode = 0666
user = www
group = www
pm = static
pm.max_children = 5
pm.start_servers = 20
pm.min_spare_servers = 10
pm.max_spare_servers = 40
request_terminate_timeout = 300
request_slowlog_timeout = 10
slowlog = /data/logs/php/phpslow2.log

[www3]
listen = /tmp/php-cgi3.sock
listen.backlog = 8192
listen.allowed_clients = 127.0.0.1
listen.owner = www
listen.group = www
listen.mode = 0666
user = www
group = www
pm = static
pm.max_children = 5
pm.start_servers = 20
pm.min_spare_servers = 10
pm.max_spare_servers = 40
request_terminate_timeout = 300
request_slowlog_timeout = 10
slowlog = /data/logs/php/phpslow3.log
EOF

 

# 添加php-fpm至服务列表并设置开机自启。

chkconfig --add php-fpm 
chkconfig --list php-fpm 
chkconfig php-fpm on

 

# 启动php-fpm

/etc/init.d/php-fpm start

 

#自行选择是否使用下面php.ini 配置

cat > /data/app/php/lib/php.ini << EOF
[userconfig]
environment = ONLINE
gd.jpeg_ignore_warning = 1

[PHP]
engine = On
short_open_tag = On
asp_tags = Off
precision = 14
output_buffering = 4096
zlib.output_compression = Off
implicit_flush = Off
zend.enable_gc = On
expose_php = Off

max_execution_time = 30
max_input_vars = 1000
max_input_time = 65

max_input_nesting_level = 32
memory_limit = 128M

error_reporting = E_ALL & ~E_NOTICE

display_errors = Off
display_startup_errors = Off
log_errors = On
error_log = /tmp/phperror.log

variables_order = "GPCS"
request_order = "GP"
post_max_size = 50M

register_argc_argv = Off
auto_globals_jit = On

default_mimetype = "text/html"
default_charset = "UTF-8"

enable_dl = Off

file_uploads = On
upload_max_filesize = 50M
max_file_uploads = 20
upload_tmp_dir = /data/phptmp

allow_url_fopen = On
allow_url_include = Off

default_socket_timeout = 60

extension_dir = "/data/app/php/lib/php/extensions/no-debug-non-zts-20121212/"
zend_extension=opcache.so
extension=memcache.so
extension=redis.so

date.timezone = Asia/Shanghai

[Session]
session.save_handler = files
;session.save_handler = memcache
;session.save_path = tcp://127.0.0.1:10007
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.cache_limiter = nocache
session.cache_expire = 180
session.use_trans_sid = 0
session.hash_function = 0
session.hash_bits_per_character = 5

[opcache]
opcache.enable=1
opcache.enable_cli=0
opcache.memory_consumption=128
opcache.interned_strings_buffer=4
opcache.max_accelerated_files=2000
opcache.max_wasted_percentage=5
opcache.use_cwd=1
opcache.validate_timestamps=1
opcache.revalidate_freq=2
opcache.revalidate_path=1
opcache.save_comments=0
opcache.load_comments=0
opcache.fast_shutdown=0
opcache.enable_file_override=0

EOF

 

posted @ 2019-07-26 19:08  斌-逸风  阅读(316)  评论(0编辑  收藏  举报