分类: Linux
Centos7.6快速编译安装Nginx-1.14.2 及Php7.3.0

PHP7.3.png
操作系统版本:

cat /etc/redhat-release 
CentOS Linux release 7.6.1810 (Core) 

安装扩展包并更新系统内核:

yum install epel-release -y
yum update -y

安装基础依赖组件

yum install wget vim pcre pcre-devel openssl openssl-devel libicu-devel gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel ncurses ncurses-devel curl curl-devel krb5-devel libidn libidn-devel openldap openldap-devel nss_ldap jemalloc-devel cmake boost-devel bison automake libevent libevent-devel gd gd-devel libtool* libmcrypt libmcrypt-devel mcrypt mhash libxslt libxslt-devel readline readline-devel gmp gmp-devel libcurl libcurl-devel openjpeg-devel openssl openssl-devel sqlite sqlite-devel oniguruma oniguruma-devel -y

创建用户和组,下载php安装包解压:

groupadd www
useradd -g www www
cd /opt
# 添加搜索路径到配置文件
echo '/usr/local/lib64
/usr/local/lib
/usr/lib
/usr/lib64'>>/etc/ld.so.conf
ldconfig -v # 更新配置
cp -frp /usr/lib64/libldap* /usr/lib/   #编译ldap组件的时候需要

卸载老版本的libzip

yum remove libzip

下载安装libzip-1.2.0

wget https://libzip.org/download/libzip-1.3.2.tar.gz
tar -zxvf libzip-1.3.2.tar.gz
cd libzip-1.3.2
./configure
make && make install
export PKG_CONFIG_PATH="/usr/local/lib/pkgconfig/"

tar xvf jpegsrc.v9d.tar.gz
cd jpeg-9d
./configure --prefix=/usr/local/jpeg --enable-shared
make
make install

tar xvf freetype-2.9.1.tar.gz
cd freetype-2.9.1
./configure --prefix=/usr/local/freetype --enable-shared
make
make install

wget http://cn2.php.net/distributions/php-7.3.0.tar.gz
tar xvf php-7.3.0.tar.gz
cd php-7.3.0

编译:

ldconfig -v  #更新ld缓存文件
./configure --prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \
--enable-mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--enable-mysqlnd-compression-support \
--with-iconv-dir \
--with-freetype=/usr/local/freetype \
--with-jpeg=/usr/local/jpeg \
--with-webp \
--with-xpm \
--with-zlib \
--with-libxml \
--enable-xml \
--disable-rpath \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curl \
--enable-mbregex \
--enable-mbstring \
--enable-intl \
--enable-ftp \
--enable-gd \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--with-zip \
--enable-soap \
--with-gettext \
--disable-fileinfo \
--enable-opcache \
--with-pear \
--enable-maintainer-zts \
--with-ldap=shared \
--without-gdbm
安装:

make -j 8                # -j 参数根据服务器CPU核数修改,可大幅提高编译速度
make install

配置php.ini文件:

cp php.ini-development /usr/local/php/etc/php.ini
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf

修改php.ini参数:(其他参数根据实际场景修改)

vim /usr/local/php/etc/php.ini
expose_php = Off
short_open_tag = ON
max_execution_time = 300
max_input_time = 300
memory_limit = 128M
post_max_size = 32M
date.timezone = Asia/Shanghai
mbstring.func_overload=2
extension_dir = "/usr/local/php/lib/php/extensions/no-debug-zts-20170718/"
extension = "ldap.so"

OPcache 缓存参数:

[opcache]
zend_extension="opcache.so"
opcache.enable=1
opcache.enable_cli=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=10000
opcache.max_wasted_percentage=5
opcache.use_cwd=0
opcache.validate_timestamps=1
opcache.revalidate_freq=60
opcache.revalidate_path=0
opcache.enable_file_override=0
opcache.fast_shutdown=1
#opcache.optimization_level=0xffffffff
opcache.max_file_size=0
#opcache.consistency_checks=0
#opcache.force_restart_timeout=180
opcache.error_log=/usr/local/php/var/log/opcache.log
opcache.log_verbosity_level=1

php安全禁用函数:

disable_functions = passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server,escapeshellcmd,dll,popen,disk_free_space,checkdnsrr,checkdnsrr,getservbyname,getservbyport,disk_total_space,posix_ctermid,posix_get_last_error,posix_getcwd, posix_getegid,posix_geteuid,posix_getgid, posix_getgrgid,posix_getgrnam,posix_getgroups,posix_getlogin,posix_getpgid,posix_getpgrp,posix_getpid, posix_getppid,posix_getpwnam,posix_getpwuid, posix_getrlimit, posix_getsid,posix_getuid,posix_isatty, posix_kill,posix_mkfifo,posix_setegid,posix_seteuid,posix_setgid, posix_setpgid,posix_setsid,posix_setuid,posix_strerror,posix_times,posix_ttyname,posix_uname

配置www.conf

[www]
user = www
group = www
listen = /usr/local/php/var/run/php-cgi.sock
listen.backlog = -1
listen.owner = www
listen.group = www
listen.mode = 0660
listen.allowed_clients = 127.0.0.1
pm = dynamic
pm.max_children = 300
pm.start_servers = 150
pm.min_spare_servers = 150
pm.max_spare_servers = 300
slowlog = /usr/local/php/var/log/slow.log
request_slowlog_timeout = 60
request_terminate_timeout = 180

配置php-fpm.conf

[global]
pid = /usr/local/php/var/run/php-fpm.pid
error_log = /usr/local/php/var/log/php-fpm.log
log_level = error
include=/usr/local/php/etc/php-fpm.d/*.conf

创建php-fpm服务启动脚本:

vim /usr/lib/systemd/system/php-fpm.service

[Unit]
Description=The PHP FastCGI Process Manager
After=syslog.target network.target

[Service]
Type=simple
PIDFile=/usr/local/php/var/run/php-fpm.pid
ExecStart=/usr/local/php/sbin/php-fpm --nodaemonize --fpm-config /usr/local/php/etc/php-fpm.conf
ExecReload=/bin/kill -USR2 $MAINPID

[Install]
WantedBy=multi-user.target

启动php-fpm服务并、开机自启动:

systemctl start php-fpm.service
systemctl enable php-fpm.service

安装Zlib、Pcre、Nginx1.4.2及组件:

cd /opt
wget https://zlib.net/fossils/zlib-1.2.11.tar.gz
tar xvf zlib-1.2.11.tar.gz
cd zlib-1.2.11
./configure --prefix=/usr/local/zlib
make
make install

wget https://ftp.pcre.org/pub/pcre/pcre-8.42.tar.gz
tar xvf pcre-8.42.tar.gz
cd pcre-8.42
./configure --prefix=/usr/local/pcre --libdir=/usr/local/lib/pcre --includedir=/usr/local/include/pcre
make
make install

wget http://nginx.org/download/nginx-1.14.2.tar.gz
tar xvf nginx-1.14.2.tar.gz
cd nginx-1.14.2
./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_realip_module --with-http_gzip_static_module --with-http_ssl_module --with-zlib=/opt/zlib-1.2.11 --with-pcre=/opt/pcre-8.42
make
make install

配置nginx.conf (完整配置文件)

user  www;
worker_processes  8;

error_log  logs/error.log  notice;

pid        logs/nginx.pid;


events {
    worker_connections  65535;
}

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_tokens off;
    include gzip.conf;

    server {
        listen       80;
        server_name  localhost;


        access_log  logs/host.access.log  main;

        location / {
            root   /data/wwwroot;
            index  index.html index.htm index.php;
        }

        error_page  404              /404.html;

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        location ~* \.php$ {
            root           /data/wwwroot;
            fastcgi_pass    unix:/usr/local/php/var/run/php-cgi.sock; #使用sock连接php服务
        fastcgi_index   index.php;
        fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include         fastcgi_params;
        }

    }
}

启动测试nginx.conf并启动nginx:

/usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
/usr/local/nginx/sbin/nginx

将nginx加入开机启动:

echo "/usr/local/nginx/sbin/nginx" >> /etc/rc.d/rc.local 
chmod +x /etc/rc.d/rc.local 

至此php7+nginx编译安装配置全部完毕。


相关博文:

发表新评论