LAMP【CentOS 7+MySQL5.7+PHP7.2+Apache2.4】环境搭建与配置

1.安装MYSQL

  • 下载软件包,这里使用的是免编译安装包,建议存放在/usr/local/src/目录中,这里我的安装包是64位;可以使用uname -i 查看系统版本信息。
    [root@localhost ~]# uname -i
    x86_64
    [root@localhost ~]# cd /usr/local/src/
    [root@localhost src]# wget http://mirrors.sohu.com/mysql/MySQL-5.7/mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz
  • 检查本地是否安装了mysql
    [root@localhost src]# rpm -qa | grep mysql
  • 如果已经安装了mysql,卸载mysql
    rpm -e 已存在mysql全名
  • 解压文件到/usr/local/下,重新命名mysql
    [root@localhost src]# tar -zxvf mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz -C  /usr/local/
    [root@localhost src]# cd /usr/local/
    [root@localhost local]# mv mysql-5.7.17-linux-glibc2.5-x86_64/ mysql
  • 建立mysql用户,启动mysql需要用该用户
    [root@localhost local]# useradd -s /sbin/nologin mysql
  • 创建一个存放mysql数据文件的目录
    [root@localhost local]# mkdir -p /data/mysql
    
  • 更改权限,不更改后续
    [root@localhost local]# chown -R mysql:mysql /data/mysql/
  • 初始化mysql数据库,basedir是mysql包所在路径,datadir是定义存放数据库的位置,默认情况下,错误日志也会记录在这个目录中;此时会自动生成数据库用户的登录密码。
    [root@localhost bin]# cd /usr/local/mysql/bin/
    [root@localhost bin]# ./mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql
    2018-03-16T10:23:01.715827Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
    2018-03-16T10:23:42.354610Z 0 [Warning] InnoDB: New log files created, LSN=45790
    2018-03-16T10:23:48.611642Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
    2018-03-16T10:23:48.867955Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 1da3bfed-2904-11e8-bba0-000c295a487b.
    2018-03-16T10:23:48.974095Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
    2018-03-16T10:23:49.049147Z 1 [Note] A temporary password is generated for root@localhost: hr%sHeAaw1/f
  •  配置mysql;默认情况系统默认有/etc/my.cnf,所以会提示是否覆盖,直接按“y”即可。
    root@localhost support-files]# cd /usr/local/mysql/support-files/
    [root@localhost support-files]# cp my-default.cnf  /etc/my.cnf
    cp: overwrite ‘/etc/my.cnf’? y
    
  • 打开/etc/my.cnf修改文件;port定义mysql服务监听的端口,如果定义默认就是3306。server_id定义mysql服务监听该mysql服务的ID号,该参数用于主从配置的时候。socket定义mysql服务监听的套接字地址
    # For advice on how to change settings please see
    # http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
    # *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
    # *** default location during install, and will be replaced if you
    # *** upgrade to a newer version of MySQL.
    
    [mysqld]
    
    # Remove leading # and set to the amount of RAM for the most important data
    # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
    # innodb_buffer_pool_size = 128M
    
    # Remove leading # to turn on a very important data integrity option: logging
    # changes to the binary log between backups.
    # log_bin
    
    # These are commonly set, remove the # and set as required.
     basedir = /usr/local/mysql
     datadir = /data/mysql
     port = 3306
     server_id = 128
     socket = /tmp/mysql.sock
    
    # Remove leading # to set options mainly useful for reporting servers.
    # The server defaults are faster for transactions and fast SELECTs.
    # Adjust sizes as needed, experiment to find the optimal values.
     join_buffer_size = 128M
     sort_buffer_size = 2M
     read_rnd_buffer_size = 2M 
    
    sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
  • 复制mysql.server到/etc/init.d/下,修改启动脚本文件。修改处datadir=/data/mysql。修改其属性
    [root@localhost support-files]# cp ./mysql.server /etc/init.d/mysqld
    [root@localhost support-files]# chmod 755 /etc/init.d/mysqld
    [root@localhost support-files]# vim /etc/init.d/mysqld
    # MySQL daemon start/stop script.
    
    # Usually this is put in /etc/init.d (at least on machines SYSV R4 based
    # systems) and linked to /etc/rc3.d/S99mysql and /etc/rc0.d/K01mysql.
    # When this is done the mysql server will be started when the machine is
    # started and shut down when the systems goes down.
    
    # Comments to support chkconfig on RedHat Linux
    # chkconfig: 2345 64 36
    # description: A very fast and reliable SQL database engine.
    
    # Comments to support LSB init script conventions
    ### BEGIN INIT INFO
    # Provides: mysql
    # Required-Start: $local_fs $network $remote_fs
    # Should-Start: ypbind nscd ldap ntpd xntpd
    # Required-Stop: $local_fs $network $remote_fs
    # Default-Start:  2 3 4 5
    # Default-Stop: 0 1 6
    # Short-Description: start and stop MySQL
    # Description: MySQL is a very fast and reliable SQL database engine.
    ### END INIT INFO
    
    # If you install MySQL on some other places than /usr/local/mysql, then you
    # have to do one of the following things for this script to work:
    #
    # - Run this script from within the MySQL installation directory
    # - Create a /etc/my.cnf file with the following information:
    #   [mysqld]
    #   basedir=<path-to-mysql-installation-directory>
    # - Add the above to any other configuration file (for example ~/.my.ini)
    #   and copy my_print_defaults to /usr/bin
    # - Add the path to the mysql-installation-directory to the basedir variable
    #   below.
    #
    # If you want to affect other MySQL variables, you should make your changes
    # in the /etc/my.cnf, ~/.my.cnf or other MySQL configuration files.
    
    # If you change base dir, you must also change datadir. These may get
    # overwritten by settings in the MySQL configuration files.
    
    basedir=
    datadir= /data/mysql
    
    
  • 将mysqld服务加入到系统服务列表中,使其开机启动。
    [root@localhost support-files]# chkconfig --add mysqld
    [root@localhost support-files]# chkconfig mysqld on
  • 启动服务。
    [root@localhost support-files]# systemctl start mysqld
  • 查看mysql进程和端口监听是否正常,正常则成功安装和配置了mysql。

    [root@192 bin]# ps aux | grep mysqld
    root       4837  0.0  0.0  11764  1592 pts/0    S    22:47   0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/192.168.183.130.pid
    mysql      5043  0.2 10.4 1248636 195108 pts/0  Sl   22:47   0:05 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql/192.168.183.130.err --pid-file=/data/mysql/192.168.183.130.pid --socket=/tmp/mysql.sock --port=3306
    root       5917  0.0  0.0 112644   952 pts/0    S+   23:24   0:00 grep --color=auto mysqld
    [root@192 bin]# netstat -lnp | grep 3306
    tcp6       0      0 :::3306                 :::*                    LISTEN      5043/mysqld   


2.安装PHP

  • 下载PHP源码包。
    [root@192 ~]# cd /usr/local/src/
    [root@192 src]# wget http://mirrors.sohu.com/php/php-7.2.3.tar.gz
  • 解压源码包。
    root@192 src]# tar -zxvf php-7.2.3.tar.gz  -C /usr/local
  • 创建帐号,该帐号用来运行php-fpm服务。在LNMP环境中,PHP以一个服务php-fpm的形式出现,独立存在于Linux系统中
    [root@192 src]# useradd -s /sbin/nologin www
  • 安装PHP依赖组件(包括Nignx依赖)
    yum -y 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
  • 配置编译选项。
    [root@192 php] ./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 \
    --with-libmbfl \
    --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 \
    --without-gdbm \
  • 编译安装。
    [root@192 php] make && make install
    
  • 配置php.ini文件。
    [root@192 php] cp php.ini-development /usr/local/php/etc/php.ini
    [root@192 php] cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
    [root@192 php] cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
    [root@192 php] vim /usr/local/php/etc/php.ini
    expose_php = Off
    short_open_tag = ON
    max_execution_time = 60
    max_input_time = 60
    memory_limit = 128M
    post_max_size = 32M
    date.timezone = Asia/Shanghai
    mbstring.func_overload=2
  • 取消以下注释并修改优化其参数。
    [root@192 php]# vim /usr/local/php/etc/php-fpm.d/www.conf
    listen.owner = www
    listen.group = www
    listen.mode = 0660
    listen.allowed_clients = 127.0.0.1
    pm = dynamic
    listen.backlog = -1
    pm.max_children = 50
    pm.start_servers = 20
    pm.min_spare_servers = 5
    pm.max_spare_servers = 35
    
  • 设置php-fpm自动启动。
    [root@192 php]# cp /usr/local/php/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
    [root@192 php]# chmod +x /etc/init.d/php-fpm 
    [root@192 php]# chkconfig --add php-fpm
    [root@192 php]# chkconfig php-fpm on
  • 启动php-fpm。
    [root@192 php]# systemctl start php-fpm

3.安装Apache

  • 下载软件安装包。
    [root@localhost ~]# cd /usr/local/src/
    [root@localhost src]# wget http://mirrors.sohu.com/mysql/MySQL-5.7/mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz
    [root@localhost src]# tar -zxvf httpd-2.4.33.tar.gz
  • 下载安装Apache的依赖包。
    [root@localhost src]# tar -zxvf apr-1.6.3
    [root@localhost src]# tar -zxvf apr-util-1.6.0
    [root@localhost src]# cd apr-1.6.3
    [root@localhost apr-1.6.3]# ./configure --prefix=/usr/local/apr
    [root@localhost src]# cd apr-util-1.6.0
    [root@localhost apr-util-1.6.0]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr

  • 配置选项。
    [root@localhost apr-util-1.6.0]# cd /usr/local/src/httpd-2.4.33/
    [root@localhost httpd-2.4.33]# ./configure \
    --prefix=/usr/local/apache \
    --with-apr=/usr/local/apr \
    --with-apr-util=/usr/local/apr-util \
    --enable-so \
    --enable-mods-shared=most
  • 编译安装。
    [root@localhost httpd-2.4.33]# make && make install
  • 配置Httpd。
    [root@localhost apr-util-1.6.0]# vim /usr/local/apache/conf/httpd.conf
    1.找到"#ServerName www.example.com:80",将"#"去掉;
    2.找到"AddType application/x-gzip .gz .tgz",在下面加上"AddType application/x-httpd-php .php";
    3.<Directory />
        AllowOverride none
        Require all granted
      </Directory>
      将"Require all died"修改为"Require all granted";
    4.<IfModule dir_module>
        DirectoryIndex index.html index.php
      </IfModule>
      将"DirectoryIndex index.html"修改为"DirectoryIndex index.html index.php";
  • 测试配置文件是否正确。出现"Syntax OK"表示正确。
    [root@localhost httpd-2.4.33]# /usr/local/apache/bin/apachectl -t
  • 设置Httpd开机自启
    [root@localhost apr-util-1.6.0]# cd /usr/local/src/httpd-2.4.33/
    [root@localhost httpd-2.4.33]# ./configure \ [root@localhost httpd-2.4.33]# cp /usr/local/apache/bin/apachectl /etc/init.d/httpd [root@localhost httpd-2.4.33]# vim /etc/init.d/httpd 在"#!/bin/sh"下面加上下面两条,"#"号必须有;这样后面"chkconfig"操作不会出错。 #chkconfig: 2345 10 90 #description: Activates/Deactivates Apache Web Server [root@localhost httpd-2.4.33]# chkconfig --add httpd [root@localhost httpd-2.4.33]# chkconfig httpd on
  • 启动Httpd。
    [root@localhost httpd-2.4.33]# systemctl start httpd
  • 测试是否正确解析PHP。
    [root@localhost httpd-2.4.33]# vim /usr/local/apache/htdocs/test.php
    <?php
        echo "php scripts\n";
    ?>
    保存退出
    [root@localhost httpd-2.4.33]# php /usr/local/apache/htdocs/test.php
    php scripts

猜你喜欢

转载自blog.csdn.net/qq_34272964/article/details/79716548