Linux

LNMP指的是Linux系统下Nginx+MySQL+PHP,是当下最流行的网站服务器架构之一。今天来总结下如何使用源码包手动编译安装LNMP环境。

系统环境

系统版本:CentOS Linux release 7.2.1511 (Core)

  1. 关闭iptables

    [root@localhost ~]# chkconfig  iptables off
    [root@localhost ~]# iptables -F
    [root@localhost ~]# chkconfig  --list|grep iptables
    iptables     0:off    1:off    2:off    3:off    4:off    5:off    6:off
  2. 关闭selinux

    [root@localhost ~]# setenforce 0
    [root@localhost ~]# getenforce
    Permissive
    [root@localhost ~]# sed  -i  "s/SELINUX=enforcing/SELINUX=disabled/g"  /etc/selinux/config

目录规划

软件源码包路径:/software/lnmp

源码包编译安装路径:/usr/local/$software_name

数据库数据目录:/data/mariadb

安装NGINX

  1. 安装相关依赖

    yum -y install gcc gcc-c++ make kernel-headers glibc-headers zlib-devel 
    yum install -y pcre pcre-devel openssl openssl-devel
  2. 编译安装nginx

    mdkir -p /software/lnmp
    cd /software/lnmp
    groupadd www
    useradd -s /sbin/nologin -M -g www www
    wget -q http://nginx.org/download/nginx-1.12.2.tar.gz
    tar xf nginx-1.12.2.tar.gz
    cd nginx-1.12.2
    ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module ---with-http_sub_module
    make && make install
    cd ../
    ln -sf /usr/local/nginx/sbin/nginx /usr/bin/nginx
  3. 添加启动脚本

    cat >/etc/init.d/nginx <<'EOF'
    #! /bin/sh
    # chkconfig: 2345 55 25
    # Description: Startup script for nginx webserver on Debian. Place in /etc/init.d and
    # run 'update-rc.d -f nginx defaults', or use the appropriate command on your
    # distro. For CentOS/Redhat run: 'chkconfig --add nginx'
    
    ### BEGIN INIT INFO
    # Provides:          nginx
    # Required-Start:    $all
    # Required-Stop:     $all
    # Default-Start:     2 3 4 5
    # Default-Stop:      0 1 6
    # Short-Description: starts the nginx web server
    # Description:       starts nginx using start-stop-daemon
    ### END INIT INFO
    
    # Author:   licess
    
    PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
    NAME=nginx
    NGINX_BIN=/usr/local/nginx/sbin/$NAME
    CONFIGFILE=/usr/local/nginx/conf/$NAME.conf
    PIDFILE=/usr/local/nginx/logs/$NAME.pid
    if [ -s /bin/ss ]; then
        StatBin=/bin/ss
    else
        StatBin=/bin/netstat
    fi
    
    case "$1" in
        start)
            echo -n "Starting $NAME... "
    
            if $StatBin -tnpl | grep -q nginx;then
                echo "$NAME (pid `pidof $NAME`) already running."
                exit 1
            fi
    
            $NGINX_BIN -c $CONFIGFILE
    
            if [ "$?" != 0 ] ; then
                echo " failed"
                exit 1
            else
                echo " done"
            fi
            ;;
    
        stop)
            echo -n "Stoping $NAME... "
    
            if ! $StatBin -tnpl | grep -q nginx; then
                echo "$NAME is not running."
                exit 1
            fi
    
            $NGINX_BIN -s stop
    
            if [ "$?" != 0 ] ; then
                echo " failed. Use force-quit"
                exit 1
            else
                echo " done"
            fi
            ;;
    
        status)
            if $StatBin -tnpl | grep -q nginx; then
                PID=`pidof nginx`
                echo "$NAME (pid $PID) is running..."
            else
                echo "$NAME is stopped."
                exit 0
            fi
            ;;
    
        force-quit|kill)
            echo -n "Terminating $NAME... "
    
            if ! $StatBin -tnpl | grep -q nginx; then
                echo "$NAME is is stopped."
                exit 1
            fi
    
            kill `pidof $NAME`
    
            if [ "$?" != 0 ] ; then
                echo " failed"
                exit 1
            else
                echo " done"
            fi
            ;;
    
        restart)
            $0 stop
            sleep 1
            $0 start
            ;;
    
        reload)
            echo -n "Reload service $NAME... "
    
            if $StatBin -tnpl | grep -q nginx; then
                $NGINX_BIN -s reload
                echo " done"
            else
                echo "$NAME is not running, can't reload."
                exit 1
            fi
            ;;
    
        configtest)
            echo -n "Test $NAME configure files... "
    
            $NGINX_BIN -t
            ;;
    
        *)
            echo "Usage: $0 {start|stop|restart|reload|status|configtest|force-quit|kill}"
            exit 1
            ;;
    
    esac
    
    EOF
    
    chmod +x /etc/init.d/nginx
    chkconfig --add nginx
    chkconfig nginx on
    service nginx start

安装PHP

  1. 安装依赖

    yum install -y gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libpng libpng-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses curl openssl-devel db4-devel libXpm-devel libX11-devel gmp-devel readline-devel libxslt-devel expat-devel xmlrpc-c libcurl libcurl-devel perl perl-devel libicu-devel
  2. 编译安装加密扩展库libmcrypt

    # centos7.x
    rpm -Uvh http://dl.fedoraproject.org/pub/epel/7/x86_64/Packages/e/epel-release-7-11.noarch.rpm
    yum install -y libmcrypt libmcrypt-devel
  3. 编译安装PHP

    cd /software/lnmp
    wget http://cn2.php.net/distributions/php-5.6.30.tar.gz
    tar xf php-5.6.30.tar.gz 
    cd php-5.6.30 
    ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-config-file-scan-dir=/usr/local/php/conf.d --enable-fpm --with-fpm-user=www --with-fpm-group=www --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-iconv-dir --with-freetype-dir=/usr/local/freetype --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --enable-mbregex --enable-mbstring --with-mcrypt --enable-ftp --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --with-gettext --disable-fileinfo --enable-opcache --enable-intl --with-xsl
    
    make && make install
    cd ../
  4. 创建php配置文件,建立软链

php.ini:php引擎配置文件

php-fpm.conf:php-fpm进程服务配置文件

命令如下:

cd /software/lnmp/php-5.6.30
\cp php.ini-production  /etc/php.ini
\cp /usr/local/php/etc/php-fpm.conf.default  /usr/local/php/etc/php-fpm.conf
\cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

ln -sf /usr/local/php/sbin/php-fpm /usr/bin/php-fpm
ln -sf /usr/local/php/bin/php /usr/bin/php
ln -sf /usr/local/php/bin/phpize /usr/bin/phpize
ln -sf /usr/local/php/bin/php-config /usr/bin/php-config

chmod +x /etc/init.d/php-fpm
chkconfig --add php-fpm
chkconfig php-fpm on
  1. 测试配置是否正确并启用服务

    [root@localhost php-5.6.30]# php-fpm -t
    [17-Aug-2017 14:04:06] NOTICE: configuration file /usr/local/php/etc/php-fpm.conf test is successful
    [root@localhost ~]# service php-fpm start
    Starting php-fpm  done
    [root@localhost ~]# ps -ef|grep php-fpm
    root      2929     1  0 14:06 ?        00:00:00 php-fpm: master process (/usr/local/php/etc/php-fpm.conf)
    www       2930  2929  0 14:06 ?        00:00:00 php-fpm: pool www
    www       2931  2929  0 14:06 ?        00:00:00 php-fpm: pool www
    root      2936  7854  0 14:06 pts/0    00:00:00 grep --color=auto php-fpm
    [root@localhost ~]# netstat -lntup|grep php
    tcp        0      0 127.0.0.1:9000          0.0.0.0:*        LISTEN      2929/php-fpm: maste
  2. 配置nginx支持PHP程序请求访问

server{}段添加以下代码:

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
..........
location ~ \.php$ {
      root           html;
      fastcgi_pass   127.0.0.1:9000;
      fastcgi_index  index.php;
      fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
      include        fastcgi_params;
}
..........
  1. 测试php解析是否生效

    [root@localhost ~]# cd /usr/local/nginx/html/
    [root@localhost html]# echo "<?php phpinfo(); ?>" >phpinfo.php
    [root@localhost html]# cat phpinfo.php 
    <?php phpinfo(); ?>
    [root@localhost html]# service nginx restart

出现下面的界面即说明php已经可以正常解析nginx的请求了。

mark

安装MySQL

  1. ​​‌‌​​​‌‌​‌​​‌‌‍​‌​‌‌‌​​‌‌‌‌​‌​‍​‌​​‌​​​‌​​​‌‌​‍​‌​‌‌​​​‌‌​​​​​‍​​‌​‌‌‌‌‌‌‌‌​​​‍​‌‌​​‌‌‌​‌‌​​‌‌‌‍​‌‌​​​‌‌‌​​​‌​‌‍​​‌‌‌‌‌‌‌‌​​‌‌‍​​‌‌​‌‌‌‌​‌​​‌​‍​‌​​​​‌​​​​​‌​‌‍‌​‌‌​​‌‌‍‌​‌‌​​​‌‍‌​‌‌​​‌​‍‌​‌​‌‌‌‌‍​​​‌‌​​​‌​‌​​​​‍​‌​​‌‌‌​‌‌‌‌‌​​‍​​​​​​​​‌‌‌​​‌​‌‍​​​​​​​‌‌‌​‌​​‌‍​‌‌‌​‌​​​​‌​‌‌‌​‍​‌​​‌​​​‌‌‌​‌‌​‍​‌‌‌​‌‌‌​​‌‌‌​‌​‍​​​​‌​​​​‌‌‌​​​‍​​‌‌‌‌‌‌‌‌​​‌​‍​​​​​​​​‌‌‌‌​​‌‌‍​​​‌​‌​‌‌​​‌‌‌​‍‌​​‌‌‌‌​‍‌​​‌‌​‌‌‍‌​​‌​​‌​‍‌​​‌​‌‌​‍‌​​‌​​​‌‍​‌‌​​​‌​‌‌‌​​​‌‍‌‌​​‌‌​‌‍‌‌​​‌‌‌‌‍‌‌​​‌‌‌​‍‌‌​​‌​​​‍‌‌​‌​​‌​‍‌‌​​‌‌‌‌‍‌‌​​‌​​​‍‌‌​‌​​‌​‍‌‌​​‌‌‌​‍‌‌​​‌​​‌‍​‌​‌‌​‌‌‌‌​​‌​​‍​‌‌​​​​‌​‌​​​‌‌‍​​​​​​​​‌‌‌‌​​‌‌‍​‌​‌‌​​​‌‌​​​​​‍​​‌‌​‌​​‌‌‌‌​​​‍​‌​‌​​​‌‌​​‌‌‌‌‍​‌​‌​​​‌​‌‌‌‌‌‌‍​​​​​​​​‌‌‌​​‌​‌‍‌​​‌​‌‌‌‍‌​​​‌​‌‌‍‌​​​‌​‌‌‍‌​​​‌‌‌‌‍‌​​​‌‌​​‍‌‌​​​‌​‌‍‌​‌​​​‌‌‍‌​‌​​​‌‌‍‌​​​‌​​​‍‌​​​‌​​​‍‌​​​‌​​​‍‌‌​‌​​​‌‍‌​​‌​‌‌​‍‌​​‌​‌​​‍‌​​‌​‌‌​‍‌​​​‌​​​‍‌​​‌​‌‌​‍‌‌​‌​​​‌‍‌​​‌​​‌​‍‌​​‌‌​‌​‍‌​‌​​​‌‌‍‌​​‌‌‌‌​‍‌​​​‌‌​‌‍‌​​‌‌‌​​‍‌​​‌​‌‌‌‍‌​​‌​‌‌​‍‌​​​‌​​‌‍‌​​‌‌​‌​‍‌​​​‌‌​​‍‌​‌​​​‌‌‍‌‌​​‌​​‌‍‌‌​‌​​​‌‍‌​​‌​‌‌‌‍‌​​​‌​‌‌‍‌​​‌​​‌​‍‌​​‌​​‌‌

    下载软件包并编译安装

    cd /software/lnmp
    wget http://ftp.hosteurope.de/mirror/archive.mariadb.org//mariadb-10.0.30/source/mariadb-10.0.30.tar.gz
    tar zxf mariadb-10.0.30.tar.gz
    cd mariadb-10.0.30
    cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mariadb \
    -DMYSQL_DATADIR=/data/mariadb \
    -DWITH_ARIA_STORAGE_ENGINE=1 \
    -DWITH_XTRADB_STORAGE_ENGINE=1 \
    -DWITH_INNOBASE_STORAGE_ENGINE=1 \
    -DWITH_PARTITION_STORAGE_ENGINE=1 \
    -DWITH_MYISAM_STORAGE_ENGINE=1 \
    -DWITH_FEDERATED_STORAGE_ENGINE=1 \
    -DEXTRA_CHARSETS=all \
    -DDEFAULT_CHARSET=utf8mb4 \
    -DDEFAULT_COLLATION=utf8mb4_general_ci \
    -DWITH_READLINE=1 \
    -DWITH_EMBEDDED_SERVER=1 \
    -DENABLED_LOCAL_INFILE=1
    
    make && make install
  2. 创建用户且设置权限

    groupadd mariadb
    useradd -s /sbin/nologin -M -g mariadb mariadb
    chown -R mariadb:mariadb /data/mariadb
    chgrp -R mariadb /usr/local/mariadb/.
    \cp support-files/mysql.server /etc/init.d/mariadb
    chmod 755 /etc/init.d/mariadb
  3. 创建相关目录并赋权

    mkdir -p /data/mariadb
    mkdir -p /var/run/mariadb
    mkdir -p /var/log/mariadb
    chown -R mariadb:mariadb /data/mariadb/
    chown -R mariadb:mariadb /var/run/mariadb/
    chown -R mariadb:mariadb /var/log/mariadb/
  4. 修改配置文件

    cat > /etc/my.cnf<<'EOF'
    [client]
    #password   = your_password
    port        = 3306
    socket      = /tmp/mysql.sock
    
    [mysqld]
    port        = 3306
    socket      = /tmp/mysql.sock
    user    = mariadb
    basedir = /usr/local/mariadb
    datadir = /data/mariadb
    log_error = /var/log/mariadb/mariadb.err
    pid-file = /var/run/mariadb/mariadb.pid
    skip-external-locking
    key_buffer_size = 16M
    max_allowed_packet = 1M
    table_open_cache = 64
    sort_buffer_size = 512K
    net_buffer_length = 8K
    read_buffer_size = 256K
    read_rnd_buffer_size = 512K
    myisam_sort_buffer_size = 8M
    thread_cache_size = 8
    query_cache_size = 8M
    tmp_table_size = 16M
    
    explicit_defaults_for_timestamp = true
    #skip-networking
    max_connections = 500
    max_connect_errors = 100
    open_files_limit = 65535
    
    log-bin=mysql-bin
    binlog_format=mixed
    server-id   = 1
    expire_logs_days = 10
    
    default_storage_engine = InnoDB
    #innodb_file_per_table = 1
    #innodb_data_home_dir = /data/mariadb
    #innodb_data_file_path = ibdata1:10M:autoextend
    #innodb_log_group_home_dir = /data/mariadb
    #innodb_buffer_pool_size = 16M
    #innodb_log_file_size = 5M
    #innodb_log_buffer_size = 8M
    #innodb_flush_log_at_trx_commit = 1
    #innodb_lock_wait_timeout = 50
    
    [mysqldump]
    quick
    max_allowed_packet = 16M
    
    [mysql]
    no-auto-rehash
    
    [myisamchk]
    key_buffer_size = 20M
    sort_buffer_size = 20M
    read_buffer = 2M
    write_buffer = 2M
    
    [mysqlhotcopy]
    interactive-timeout
    EOF
  5. 配置环境变量

    [root@localhost ~]# cat >/etc/profile.d/mariadb.sh<<'EOF'
    > export PATH=$PATH:/usr/local/mariadb/bin/
    > EOF
    [root@localhost ~]# chmod +x /etc/profile.d/mariadb.sh
    [root@localhost ~]# source /etc/profile.d/mariadb.sh
  6. 初始化数据库

    [root@localhost ~]# /usr/local/mariadb/scripts/mysql_install_db --defaults-file=/etc/my.cnf --basedir=/usr/local/mariadb --datadir=/data/mariadb --user=mariadb
    [root@localhost ~]# service mariadb start
    [root@localhost ~]# mysql_secure_installation
    
    NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
          SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!
    
    In order to log into MariaDB to secure it, we'll need the current
    password for the root user.  If you've just installed MariaDB, and
    you haven't set the root password yet, the password will be blank,
    so you should just press enter here.
    
    Enter current password for root (enter for none):     #输入root密码,初次运行直接回车
    OK, successfully used password, moving on...
    
    Setting the root password ensures that nobody can log into the MariaDB
    root user without the proper authorisation.
    
    Set root password? [Y/n] y    #设置root密码
    New password: 
    Re-enter new password: 
    Password updated successfully!
    Reloading privilege tables..
     ... Success!
    
    By default, a MariaDB installation has an anonymous user, allowing anyone
    to log into MariaDB without having to have a user account created for
    them.  This is intended only for testing, and to make the installation
    go a bit smoother.  You should remove them before moving into a
    production environment.
    
    Remove anonymous users? [Y/n] Y    #删除匿名用户
     ... Success!
    
    Normally, root should only be allowed to connect from 'localhost'.  This
    ensures that someone cannot guess at the root password from the network.
    
    Disallow root login remotely? [Y/n] Y    #禁止root远程登录
     ... Success!
    
    By default, MariaDB comes with a database named 'test' that anyone can
    access.  This is also intended only for testing, and should be removed
    before moving into a production environment.
    
    Remove test database and access to it? [Y/n] Y    #删除test数据库
     - Dropping test database...
     ... Success!
     - Removing privileges on test database...
     ... Success!
    
    Reloading the privilege tables will ensure that all changes made so far
    will take effect immediately.
    
    Reload privilege tables now? [Y/n] Y    #重新加载权限表
     ... Success!
    
    Cleaning up...
    
    All done!  If you've completed all of the above steps, your MariaDB
    installation should now be secure.
    
    Thanks for using MariaDB!
  7. 启动mariadb数据库

    [root@localhost ~]# service maridab start
    [root@localhost ~]# mysql -uroot -p

出现以下界面就说明大功告成了!

mark

检查服务

最后来检查下各项服务:

mark

至此,手动编译LNMP环境完成,收工。