LNMP的安装配置

版权声明:版权归PHPerJiang所有 https://blog.csdn.net/qq_36558538/article/details/89156927

更换yum源为阿里源

#!/bin/bash

#备份本地yum源
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo_bak

#获取阿里的yum源配置
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

#更新cache
yum makecache

#更新
yum -y update

扩容交换空间

由于我的是学生服务器,穷啊,还没毕业,真是的!哼!所以需要扩容交换空间。有钱的老板可以跳过这一步。

#!/bin/bash

#服务器上的交换分区建议创建物理内存的一般即可
dd if=/dev/zero of=/swapfile bs=1k count=1024000
mkswap /swapfile
swapon /swapfile

#在文件系统设置
echo "/swapfile  swap  swap    defaults 0 0" >> /etc/fstab

sysctl vm.swappiness=100

这个问题出现在编译安装mysql的时候,由于编译安装需要大量内存,直接把我的小服务器给中断了,只好这么做了。编译安装时出现的错误提示是internal compiler error: Killed (program cc1plus)意思就是内存不足了,给你中断了。

编译安装PHP(7.3.0)

  1. 安装依赖(安装了nginx+php的依赖,满足大部分场景,需要扩展请自行添加依赖)
    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 -y
  2. 创建用户和用户组

    groupadd www
    useradd -g www www
  3. 添加搜索路径到配置文件

    cd /opt
    
    echo '/usr/local/lib64
    /usr/local/lib
    /usr/lib
    /usr/lib64'>>/etc/ld.so.conf

    更新配置文件

    ldconfig -v

    创建编译ldap组件时需要的配置文件

    cp -frp /usr/lib64/libldap* /usr/lib/
  4. 下载PHP

    #-c 为断点续链命令
    wget -c https://www.php.net/distributions/php-7.3.4.tar.gz
    
    #解压
    tar -zxvf php-7.3.4.tar.gz
    
    #切入目录
    cd php-7.3.4
    
    
  5. ./configure校验文件和依赖

    #更新ld缓存文件
    
    ldconfig -v 
    
    #校验
    
    #.configure检查
    ./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-dir \
    --with-jpeg-dir \
    --with-png-dir \
    --with-zlib \
    --with-libxml-dir \
    --enable-xml \
    --disable-rpath \
    --enable-bcmath \
    --enable-shmop \
    --enable-sysvsem \
    --enable-inline-optimization \
    --with-curl \
    --enable-mbregex \
    --enable-mbstring \
    --enable-intl \
    --enable-ftp \
    --with-gd \
    --enable-gd-jis-conv \
    --with-openssl \
    --with-mhash \
    --enable-pcntl \
    --enable-sockets \
    --with-xmlrpc \
    --enable-zip \
    --enable-soap \
    --with-gettext \
    --disable-fileinfo \
    --enable-opcache \
    --with-pear \
    --enable-maintainer-zts \
    --with-ldap=shared \
    --without-gdbm \
    --enable-static \
    --with-kerberos \
    --with-libdir=lib64 \
    --with-pcre-regex \
    --with-pdo-sqlite \
    --with-xsl 
  6. 可能出现的问题

    1. libzip >=0.11 提示 意思是版本过低 需要下高版本的依赖http://www.kwx.gd/PHPEnvironment/CetnOS-libzip.html

    2. configure: error: libxml2 not found. Please check your libxml2 installation.  

      yum install -y libxml2-devel
    3. configure: error: Please reinstall the BZip2 distribution

      yum install -y bzip2-devel
    4. configure: error: cURL version 7.15.5 or later is required to compile php with cURL support

      yum install -y curl-devel
    5. configure: error: jpeglib.h not found.

      yum install -y libjpeg-devel
    6. checking for libzip... configure: error: system libzip must be upgraded to version >= 0.11

      #先删除旧版本
      yum remove -y libzip
      
      #下载编译安装
      wget https://nih.at/libzip/libzip-1.2.0.tar.gz
      tar -zxvf libzip-1.2.0.tar.gz
      cd libzip-1.2.0
      ./configure
      make && make install
  7. 编译安装

    make && make install

    可能会出现的问题 usr/local/include/zip.h:59:21: fatal error: zipconf.h: No such file or directory

    cp /usr/local/lib/libzip/include/zipconf.h /usr/local/include/zipconf.h
  8. 配置php.ini文件

    #迁移文件至php安装目录下
    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文件
    
    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"
  9. 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
  10. 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
  11. 配置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
  12. 配置php-fpm.conf

    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
  13. 创建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
  14. 启动php-fpm服务并、开机自启动:

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

编译安装Nginx

  1. 安装依赖

    yum install -y gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
  2. 下载nginx

    wget -c http://nginx.org/download/nginx-1.14.2.tar.gz

    http://nginx.org/en/download.html

  3. ./configure检查文件及依赖

    ./configure \
    --prefix=/usr/local/nginx \
    --conf-path=/usr/local/nginx/conf/nginx.conf \
    --pid-path=/usr/local/nginx/conf/nginx.pid \
    --lock-path=/var/lock/nginx.lock \
    --error-log-path=/var/log/nginx/error.log \
    --http-log-path=/var/log/nginx/access.log \
    --with-http_gzip_static_module \
    --http-client-body-temp-path=/var/temp/nginx/client \
    --http-proxy-temp-path=/var/temp/nginx/proxy \
    --http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
    --http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
    --http-scgi-temp-path=/var/temp/nginx/scgi
  4. 编译 安装  make && make install

  5. 配置nginx并支持php

    #切入nginx的配置文件
    vim /usr/local/nginx/conf/nginx.conf
    
    #修改配置
    
    user  www;   #用户所属   
    worker_processes  8;   #worker数
    
    error_log  logs/error.log  notice;
    
    pid        logs/nginx.pid;
    
    
    events {
        worker_connections  8;  #woker支持的并发连接数
    }
    
    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;
        include gzip.conf;
    
        server {
            listen       80;
            server_name  localhost;
    
    
            access_log  logs/host.access.log  main;
    
            location / {
                alias   /data/wwwroot;   #html文件目录
                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$ {
                alias   /data/wwwroot;   #html文件目录
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_index   index.php;
                fastcgi_param  SCRIPT_FILENAME  /data/www$fastcgi_script_name;
                include         fastcgi_params;
            }
    
        }
    }
  6. 开启nginx

    #测试配置文件是否正确
    /usr/local/nginx/sbin/nginx -t
    
    #启动nginx
    /usr/local/nginx/sbin/nginx
    
    #将nginx加入开机启动项
    echo "/usr/local/nginx/sbin/nginx" >> /etc/rc.d/rc.local 
    chmod +x /etc/rc.d/rc.local 
  7. 将nginx添加到service服务

    #!/bin/sh
    #
    # nginx - this script starts and stops the nginx daemin
    #
    # chkconfig: - 85 15
    # description: Nginx is an HTTP(S) server, HTTP(S) reverse \
    # proxy and IMAP/POP3 proxy server
    # processname: nginx
    # config: /usr/local/nginx/conf/nginx.conf
    # pidfile: /usr/local/nginx/logs/nginx.pid
    
    # Source function library.
    . /etc/rc.d/init.d/functions
    
    # Source networking configuration.
    . /etc/sysconfig/network
    
    # Check that networking is up.
    [ "$NETWORKING" = "no" ] && exit 0
    
    nginx="/usr/local/nginx/sbin/nginx"
    prog=$(basename $nginx)
    
    NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
    
    lockfile=/var/lock/subsys/nginx
    
    start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
    }
    
    stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
    }
    
    restart() {
    configtest || return $?
    stop
    start
    }
    
    reload() {
    configtest || return $?
    echo -n $"Reloading $prog: "
    killproc $nginx -HUP
    RETVAL=$?
    echo
    }
    
    force_reload() {
    restart
    }
    
    configtest() {
    $nginx -t -c $NGINX_CONF_FILE
    }
    
    rh_status() {
    status $prog
    }
    
    rh_status_q() {
    rh_status >/dev/null 2>&1
    }
    
    case "$1" in
    start)
    rh_status_q && exit 0
    $1
    ;;
    stop)
    rh_status_q || exit 0
    $1
    ;;
    restart|configtest)
    $1
    ;;
    reload)
    rh_status_q || exit 7
    $1
    ;;
    force-reload)
    force_reload
    ;;
    status)
    rh_status
    ;;
    condrestart|try-restart)
    rh_status_q || exit 0
    ;;
    *)
    echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
    exit 2
    esac

    service nginx start

yum安装mysql8.0

  1. 查看本机上是否有mariadb
    #查看是否安装mariadb
    rpm -qa |grep mariadb
    
    #安装了则卸载
    rpm -e mariadb-libs-5.5.56-2.el7.x86_64
    
    #可能出现依赖检测报错 则强制删除
    rpm -e --nodeps mariadb-libs-5.5.56-2.el7.x86_64
    
    
  2. 下载安装最新的mysql-repo安装包并安装

    #下载最新的mysql-rpm包
    wget -c https://dev.mysql.com/get/mysql80-community-release-el7-2.noarch.rpm
    
    #安装repo文件
    rpm -ivh mysql57-community-release-el7-11.noarch.rpm
    
    
    #执行结果会在/etc/yum.repos.d/目录下生成两个repo文件mysql-community.repo mysql-community-source.repo
    
    #更新yum
    yum clean all
    yum makecache
  3. 使用yum安装最新版的mysql

    #查看所有版本的mysql
    yum repolist all |grep mysql
    
    #yum安装MySQL
    yum install mysql-community-server
    
    #设置开机启动
    systemctl start mysqld.service
    
  4. 获取初始密码

    #mysql在安装后会创建一个root@locahost账户,并且把初始的密码放到了/var/log/mysqld.log文件中;
    cat /var/log/mysqld.log | grep password
  5. 登录并修改密码

    #登录
    mysql -u root -p
    
    #修改密码
    ALTER USER 'root'@'localhost' IDENTIFIED BY 'Mypassword!';
    
    
    #设置mysql开机启动
    systemctl enable mysqld.service
    systemctl start mysqld.service
    
    
    #mysql常用命令
    
    #登录mysql
    mysql -u username -p
     
    #退出mysql 
    quit
     
    #启动mysql
    systemctl start mysqld.service
     
    #结束
    systemctl stop mysqld.service
     
    #重启
    systemctl restart mysqld.service
     
    #开机自启
    systemctl enable mysqld.service
     
    #查看mysql版本
    select version();
  6. 设置防火墙端口

    #关闭centos7默认防火墙firewall
    
    systemctl stop firewalld.service
    systemctl disable firewalld.service
    systemctl mask firewalld.service
    
    #安装iptables防火墙
    
    yum install iptables-services -y
    
    #设置防火墙配置文件
    vim /etc/sysconfig/iptables
    
    #在倒数第三行加入
    -A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT
    
    #启动防火墙并加入开机启动
    systemctl enable iptables.service
    systemctl start iptables.service
    

 

猜你喜欢

转载自blog.csdn.net/qq_36558538/article/details/89156927