前言

LNMP算是比较容易的一个服务了,没有使用YUM直接安装还是比较痛苦的。

安装步骤

AliSQL

  1. 安装编译源码所需库和工具
    yum install gcc gcc-c++ ncurses-devel perl 

还需要安装cmake和bison,因为已经自带了,所以不详述,详见参考文档
2. 增加mysql组和用户

groupadd mysql 
useradd -r -g mysql mysql 
  1. 建立安装目录和数据文件目录

    mkdir –p /usr/local/mysql 
    mkdir -p /data/mysqldb 
  2. 下载解压并编译源码

    git clone https://github.com/alibaba/AliSQL
    unzip AliSQL-master.zip
    cd AliSQL-master
    cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DMYSQL_DATADIR=/data/mysqldb -DMYSQL_TCP_PORT=3306 -DENABLE_DOWNLOADS=1 
    make
    make install
  3. 修改相关目录属主和数组

    chown -R mysql:mysql /usr/local/mysql
    chown -R mysql:mysql /data/mysqldb
  4. 初始化AliSQL数据库

    cd /usr/local/mysql
    scripts/mysql_install_db –user=mysql –datadir=/data/mysqldb 

成功初始化后,提示以下信息,记下来以备后用。

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:

  ./bin/mysqladmin -u root password 'new-password'
  ./bin/mysqladmin -u root -h localhost.localdomain password 'new-password'

Alternatively you can run:

  ./bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.

See the manual for more instructions.

You can start the MySQL daemon with:

  cd . ; ./bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl

  cd mysql-test ; perl mysql-test-run.pl

Please report any problems at http://bugs.mysql.com/

The latest information about MySQL is available on the web at

  http://www.mysql.com

Support MySQL by buying support/licenses at http://shop.mysql.com

New default config file was created as ./my.cnf and
will be used by default by the server when you start it.
You may edit this file to change server settings
  1. 复制AliSQL服务启动配置文件、脚本,并加入PATH
    cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf 
    cp support-files/mysql.server /etc/init.d/mysqld
    vim /etc/profile 
    

#加入这行
PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH

export PATH
source/etc/profile


8. 启动AliSQL服务并加入开机自启
```bash
service mysqld start 
chkconfig –level 35 mysqld on 
  1. 检查是否已启动
    netstat -tulnp | grep 3306 
    mysql -u root -p

Nginx

  1. 下载包并解压

    tar zxvf nginx-1.12.2.tar.gz
    cd ./nginx-1.12.2
  2. 编译安装

    ./configure  --prefix=/usr/local/nginx  --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --error-log-path=/var/log/nginx/error.log  --http-log-path=/var/log/nginx/access.log  --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock  --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --http-client-body-temp-path=/var/tmp/nginx/client/ --http-proxy-temp-path=/var/tmp/nginx/proxy/ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --http-scgi-temp-path=/var/tmp/nginx/scgi --with-pcre

    可能需要依赖文件如下

    yum install gcc gcc-c++ automake pcre pcre-devel zlip zlib-devel openssl openssl-devel 

    安装完毕后有相关的提示信息,马克以备后用

    Configuration summary
    + using system PCRE library
    + using system OpenSSL library
    + using system zlib library
    
    nginx path prefix: "/usr/local/nginx"
    nginx binary file: "/usr/local/nginx/sbin/nginx"
    nginx modules path: "/usr/local/nginx/modules"
    nginx configuration prefix: "/usr/local/nginx/conf"
    nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
    nginx pid file: "/var/run/nginx/nginx.pid"
    nginx error log file: "/var/log/nginx/error.log"
    nginx http access log file: "/var/log/nginx/access.log"
    nginx http client request body temporary files: "/var/tmp/nginx/client/"
    nginx http proxy temporary files: "/var/tmp/nginx/proxy/"
    nginx http fastcgi temporary files: "/var/tmp/nginx/fcgi/"
    nginx http uwsgi temporary files: "/var/tmp/nginx/uwsgi"
    nginx http scgi temporary files: "/var/tmp/nginx/scgi"
  3. 修改监听端口

    vim /usr/local/nginx/conf/nginx.conf
    

listen把80改为8090,以防冲突

server {
listen 8090;


4. 启动nginx
```bash
ln -sf /usr/local/nginx/sbin/nginx  /usr/sbin
# 启动
nginx
# 重载配置
nginx -s reload
# 停止
nginx -s stop

执行错误有:

  • nginx: [emerg] getpwnam("nginx") failed:表示没有nginx用户(useradd -s /sbin/nologin -M nginx)
  • nginx: [emerg] mkdir() "/var/tmp/nginx/client/" failed (2: No such file or directory):表示没有该目录(mkdir -p)
  1. 验证nginx是否开启、端口是否监听

    ps -ef | grep nginx
    netstat -antl | grep 8090

    直接访问ip:8090显示Welcome to Nginx表明安装成功

  2. 更复杂的配置详看参考文档2

PHP

  1. 下载PHP压缩包解压并进入目录

  2. 编译安装
    为了整合mysql 记得要带上--with-mysql=YOUR_MYSQL_PATH

    ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=php-fpm --with-fpm-group=php-fpm --with-mysql=/usr/local/mysql --with-libxml-dir --enable-soap --enable-gd-native-ttf --enable-ftp --enable-mbstring --enable-exif --disable-ipv6 --with-curl --with-openssl

    同样可能需要安装依赖文件,见机行事

    yum -y install libxml2 libxml2-devel curl curl-devel 

    安装后有提示信息,同样马克

    You may want to add: /usr/local/php/lib/php to your php.ini include_path
    /usr/local/src/php-5.6.0/build/shtool install -c ext/phar/phar.phar /usr/local/php/bin
    ln -s -f /usr/local/php/bin/phar.phar /usr/local/php/bin/phar
    Installing PDO headers:  /usr/local/php/include/php/ext/pdo/
  3. 修改配置文件

    cp php-5.6.0/php.ini-production /usr/local/php/etc/php.ini
    cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

    使用/usr/local/php/sbin/php-fpm -t测试是否成功,不成功则

    vim /usr/local/php/etc/php-fpm.conf
    # 将其中user和group改成系统用户
  4. 启用php-fpm

    service php-fpm start
    

#开机启动和检测是否已启动
chkconfig php-fpm on
ps aux | grep php-fpm
netstat -ant |grep 9000


5. 加入环境变量
```bash
vim /etc/profile

##添加以下两行
PATH=/usr/local/webserver/php/bin:$PATH
export PATH

source /etc/profile
# 写个代码检验一下
php test.php

更改Nginx配置

整合成LNMP,.conf的详细参数配置看参考文档2

> /usr/local/nginx/conf/nginx.conf
vim /usr/local/nginx/conf/nginx.conf


user nobody nobody;
worker_processes 2;
error_log /usr/local/nginx/logs/nginx_error.log crit;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;

events
{
    use epoll;
    worker_connections 6000;
}

http
{
    include mime.types;
    default_type application/octet-stream;
    server_names_hash_bucket_size 3526;
    server_names_hash_max_size 4096;
    log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
    '$host "$request_uri" $status'
    '"$http_referer" "$http_user_agent"';
    sendfile on;
    tcp_nopush on;
    keepalive_timeout 30;
    client_header_timeout 3m;
    client_body_timeout 3m;
    send_timeout 3m;
    connection_pool_size 256;
    client_header_buffer_size 1k;
    large_client_header_buffers 8 4k;
    request_pool_size 4k;
    output_buffers 4 32k;
    postpone_output 1460;
    client_max_body_size 10m;
    client_body_buffer_size 256k;
    client_body_temp_path /usr/local/nginx/client_body_temp;
    proxy_temp_path /usr/local/nginx/proxy_temp;
    fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
    fastcgi_intercept_errors on;
    tcp_nodelay on;
    gzip on;
    gzip_min_length 1k;
    gzip_buffers 4 8k;
    gzip_comp_level 5;
    gzip_http_version 1.1;
    gzip_types text/plain application/x-javascript text/css text/htm application/xml;

    server
    {
        # 端口
        listen 80;
        server_name localhost;
        index index.html index.htm index.php;
        # 网站根地址
        root /usr/local/nginx/html;

        location ~ \.php$ {
            include fastcgi_params;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
        }
    }
}

/usr/local/nginx/html创建test.php,内容如下

<?php
    echo phpinfo();
?>

查看http://ip/test.php是否出现phpinfo,并且在里面查看mysql是否正确配置。

参考文档