CentOS7下LNMP基础环境配置

CentOS7下LNMP基础环境配置

自己手动安装lnmp环境

  • 准备工作

    • CentOS7.8服务器一台
  • 安装nginx

    • 更新系统操作
    yum update
    
    • 安装基本依赖
    yum -y install gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel ibxml2 libxml2-devel curl-devel libjpeg-devel libpng-devel freetype-devel libmcrypt-devel sqlite-devel oniguruma-devel
    
    wget https://nginx.org/download/nginx-1.18.0.tar.gz
    
    • 解压安装包
    tar -zxvf nginx-1.18.0.tar.gz
    
    • 建立www用户和用户组
    groupadd www
    useradd -g www www
    
    • 安装配置
    ./configure \
    --user=www \
    --group=www \
    --prefix=/usr/local/nginx \
    --with-http_ssl_module \
    --with-http_stub_status_module \
    --with-http_realip_module \
    --with-threads
    
    • 进行安装
    make && make install
    
    • 设置开机自启动
    vim /etc/systemd/system/nginx.service
    
    [Unit]
    Description=nginx
    After=network.target
    
    [Service]
    Type=forking
    ExecStart=/usr/local/nginx/sbin/nginx
    ExecReload=/usr/local/nginx/sbin/nginx -s reload
    ExecStop=/usr/local/nginx/sbin/nginx -s quit
    PrivateTmp=true
    
    [Install]
    WantedBy=multi-user.target
    
    • nginx参考命令
    systemctl stats nginx 查看nginx启动状态
    systemctl start nginx 启动nginx
    systemctl restart nginx 重启nginx
    systemctl stop nginx 停止nginx
    systemctl enable nginx 设置nginx为开机自启动
    systemctl disable nginx 移除nginx开机启动项
    
  • 安装PHP8.0

    wget https://www.php.net/distributions/php-8.0.25.tar.bz2
    wget https://github.com/phpredis/phpredis/archive/5.3.2.tar.gz -O phpredis-5.3.2.tar.gz
    wget https://github.com/swoole/swoole-src/archive/v4.5.9.tar.gz -O swoole-src-4.5.9.tar.gz
    wget http://pecl.php.net/get/mcrypt-1.0.3.tgz
    wget https://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.16.tar.gz
    wget https://libzip.org/download/libzip-1.5.2.tar.xz
    wget https://github.com/Kitware/CMake/releases/download/v3.19.4/cmake-3.19.4.tar.gz
    
    • 分别解压安装
    ------- 安装cmake -------
    tar -zxvf cmake-3.19.4.tar.gz && cd cmake-3.19.4
    ./bootstrap && gmake && gmake install
    
    cmake -version
    
    ------- 安装libiconv -----
    tar zxvf libiconv-1.16.tar.gz && cd libiconv-1.16 && ./configure --prefix=/usr/local/libiconv && make && make install
    ln -s /usr/local/libiconv/lib/libiconv.so.2 /usr/lib64/libiconv.so.2
    
    ------ 安装libzip ------
    tar xvJf libzip-1.5.2.tar.xz && cd libzip-1.5.2
    mkdir build && cd build/
    cmake -DCMAKE_INSTALL_PREFIX=/usr/local/libzip ..
    make && make install
    
    ------ 安装PHP8.0 ------
    ./configure \
    --prefix=/usr/local/php8 --exec-prefix=/usr/local/php8 --bindir=/usr/local/php8/bin --sbindir=/usr/local/php8/sbin \
    --includedir=/usr/local/php8/include --libdir=/usr/local/php8/lib/php --mandir=/usr/local/php8/php/man \
    --with-config-file-path=/usr/local/php8/etc \
    --with-mysql-sock=/dev/shm/mysql.sock --with-mysqli=shared,mysqlnd --with-mhash \
    --with-openssl --with-curl \
    --with-pdo-mysql=shared,mysqlnd --with-iconv --with-zlib \
    --enable-inline-optimization \
    --disable-debug --disable-rpath --enable-shared \
    --enable-xml --enable-bcmath --enable-shmop --enable-sysvsem --enable-mbregex --enable-mbstring --enable-ftp \
    --enable-pcntl --enable-sockets --with-xmlrpc --enable-soap --without-pear --with-gettext --enable-session \
    --enable-opcache --enable-fpm --without-gdbm --disable-fileinfo --with-fpm-user=www --with-fpm-group=www
    
    make && make install
    
    • 复制配置文件
    cp php.ini-production /usr/local/php8/etc/php.ini
    cp sapi/fpm/php-fpm.service /lib/systemd/system/php-fpm.service
    cp /usr/local/php8/etc/php-fpm.conf.default /usr/local/php8/etc/php-fpm.conf
    cp /usr/local/php8/etc/php-fpm.d/www.conf.default /usr/local/php8/etc/php-fpm.d/www.conf
    
    • 配置/usr/local/php8/etc/php-fpm.conf
    pid = /run/php-fpm.pid
    error_log = /var/log/php/fpm-error.log
    include=/usr/local/php8/etc/php-fpm.d/*.conf
    
    • 配置/usr/local/php8/etc/php-fpm.d/www.conf
    ###### 设置用户和用户组
    user = www
    group = www
    
    ###### 根据nginx.conf中的配置fastcgi_pass unix:/dev/shm/php-fpm.sock;设置PHP监听
    ;listen = 127.0.0.1:9000   #####不建议使用
    listen = /dev/shm/php-fpm.sock
    listen.owner = www
    listen.group = www
    listen.mode = 0777
    listen.allowed_clients = /dev/shm/php-fpm.sock
    
    ###### 使用静态进程数max_children=内存/512
    pm = static
    pm.max_children = 200
    pm.max_requests = 0
    
    pm.status_path = /FpmStatus
    
    ###### 开启慢日志
    slowlog = /var/log/php/fpm-$pool-slow.log
    request_slowlog_timeout = 30s
    request_slowlog_trace_depth = 20
    request_terminate_timeout = 0
    
    • 配置/usr/local/php8/etc/php.ini
    ######避免PHP信息暴露在http头中
    expose_php = Off
    
    ######常用配置
    error_reporting = E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED
    display_errors = Off
    log_errors = On
    html_errors = Off
    error_log = /var/log/php/cli-error.log
    ignore_repeated_errors = On
    ignore_repeated_source = On
    
    memory_limit = 512M
    max_execution_time = 600  (0为不限制)
    max_input_time = -1
    post_max_size = 128M
    max_input_vars = 1000000
    file_uploads = On
    upload_tmp_dir = /tmp/www
    upload_max_filesize = 100M
    session.gc_maxlifetime = 14400
    default_socket_timeout = 600  (-1为不限制)
    ###### 设置PHP的扩展
    extension_dir = "/usr/local/php8/lib/php/extensions/以安装目录为准"
    zend_extension=opcache.so
    extension=mcrypt.so
    extension=mysqli.so
    extension=pdo_mysql.so
    extension=redis.so
    extension=swoole.so
    
    ###### 设置PHP的时区
    date.timezone = "Asia/Shanghai"
    
    ###### 开启opcache
    [opcache]
    opcache.enable=1
    opcache.enable_cli=1
    
    ##### 开启JIT
    opcache.jit_buffer_size=128M
    opcache.jit=1205
    opcache.huge_code_pages=1
    opcache.file_cache=/tmp
    ######设置PHP脚本允许访问的目录(需要根据实际情况配置)
    ;open_basedir = /usr/share/nginx/html;
    
    ----- 使用redis的session才需要配置 ----
    session.save_handler = redis
    session.save_path = "tcp://*****.aliyuncs.com:6379? 
    auth=******"
    
    [MySQLi]
    mysqli.reconnect = On
    
    • 在配置文件中使用到的路径或文件,配置权限
    mkdir -p /var/log/php/
    mkdir -p /var/log/php-fpm/
    mkdir -p /run/php-fpm/
    mkdir -p /var/lib/php/session
    
    touch /dev/shm/php-fpm.sock
    touch /run/php-fpm.pid
    chown www:www /dev/shm/php-fpm.sock
    chmod 777 /dev/shm/php-fpm.sock
    chown -R www:www /var/lib/php
    
配置PHP连接MySQL的端口监听文件
touch /dev/shm/mysql.sock
chmod 777 /dev/shm/mysql.sock
  • 测试php-fpm配置,设置开机自启动
测试配置
/usr/local/php8/sbin/php-fpm -t
修改文件权限
chmod 745 /lib/systemd/system/php-fpm.service
设置为开机启动
systemctl enable php-fpm.service
启动php-fpm
systemctl start php-fpm.service
  • 加入环境变量
vi /etc/profile  底部加入

export PATH=$PATH:/usr/local/php8/bin
source /etc/profile
  • 安装mysql (等待更新)
Comment