编译安装LAMP

文章说明

本文中Linux命令使用的大部分是绝对路径(若没有前面有相应的目录切换命令)安装时可以不考虑路径问题

服务器相关信息

  • 腾讯云Centos7
  • 2G内存
  • 20G硬盘

软件源码位置及软件安装的目录

源码存放目录:\usr\src\
软件安装目录:\usr\local\软件名称

这里需要说明的是源码存放源代码的位置,安装过程可以指定,软件运行时跑的是软件安装的代码

  1. 相应的最新的源码包可以在下列网址中找到
  2. ngnix
  3. http://nginx.org/download/
  4. mysql
  5. https://www.mysql.com/downloads/
  6. php
  7. http://php.net/downloads.php
  8. cmake(MySQL编译工具)
  9. https://cmake.org/download/
  10. boost(MySQL编译工具)
  11. http://nchc.dl.sourceforge.net/project/boost/boost/1.59.0/boost_1_59_0.tar.gz

编译工具及库文件(yum安装)

  1. yum install -y apr* autoconf automake bison bzip2 bzip2* cloog-ppl compat* cpp curl curl-devel fontconfig fontconfig-devel freetype freetype* freetype-devel gcc gcc-c++ gtk+-devel gd gettext gettext-devel glibc kernel kernel-headers keyutils keyutils-libs-devel krb5-devel libcom_err-devel libpng libpng-devel libjpeg* libsepol-devel libselinux-devel libstdc++-devel libtool* libgomp libxml2 libxml2-devel libXpm* libxml* libtiff libtiff* make mpfr ncurses* ntp openssl openssl-devel patch pcre-devel perl php-common php-gd policycoreutils telnet t1lib t1lib* nasm nasm* wget zlib-develyum install -y apr* autoconf automake bison bzip2 bzip2* cloog-ppl compat* cpp curl curl-devel fontconfig fontconfig-devel freetype freetype* freetype-devel gcc gcc-c++ gtk+-devel gd gettext gettext-devel glibc kernel kernel-headers keyutils keyutils-libs-devel krb5-devel libcom_err-devel libpng libpng-devel libjpeg* libsepol-devel libselinux-devel libstdc++-devel libtool* libgomp libxml2 libxml2-devel libXpm* libxml* libtiff libtiff* make mpfr ncurses* ntp openssl openssl-devel patch pcre-devel perl php-common php-gd policycoreutils telnet t1lib t1lib* nasm nasm* wget zlib-devel

MySQL编译安装

前提工作

Cmake编译安装

创建软件存放目录

  1. mkdir -p /usr/local/cmake

编译安装

  1. tar -xzvf cmake-3.8.0.tar.gz
  2. cd cmake-3.8.0/
  3. ./configure --prefix=/usr/local/cmake
  4. make && make install
  5. echo 'export PATH=$PATH:/usr/local/cmake/bin' >> /etc/profile
  6. source /etc/profile

Boost编译安装

  1. tar xzvf boost_1_59_0.tar.gz
  2. cd boost_1_59_0/
  3. ./bootstrap.sh
  4. ./b2 install

需要说明的是,这里Boost的安装可以不指定目录(不进行相关目录的创建),一些配置参数可以通过./b2 --help命令看到
这里说明一下,默认存放位置是/usr/local/下;库文件存放在/usr/local/bin;头文件存放到/usr/local/include

MySQL用户及权限的配置

创建MySQL用户和用户组

  1. groupadd mysql
  2. useradd -g mysql mysql -s /bin/false

创建软件存放目录

  1. mkdir -p /usr/local/mysql

MySQL数据存放目录

  1. mkdir -p /var/mysql/data

创建日志存放目录

  1. mkdir -p /usr/local/mysql/log

对数据存放目录授予权限

  1. chown -R mysql:mysql /var/mysql/

对日志目录授予权限

  1. chown -R mysql:mysql /usr/local/mysql/log

开始真正的MySQL的编译安装

  1. tar -zxvf mysql-boost-5.7.18.tar.gz
  2. cd mysql-5.7.18
  3. cmake \
  4. -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
  5. -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
  6. -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci \
  7. -DWITH_INNOBASE_STORAGE_ENGINE=1 \
  8. -DWITH_ARCHIVE_STORAGE_ENGINE=1 \
  9. -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
  10. -DMYSQL_DATADIR=/var/mysql/data\
  11. -DMYSQL_TCP_PORT=3306\
  12. -DENABLE_DOWNLOADS=1
  13. make && make install

Cmake相关参数说明

选项 含义
DCMAKE_INSTALL_PREFIX 安装目录
DMYSQL_UNIX_ADDR sock文件路径
DDEFAULT_CHARSET 默认字符集
DWITH_INNOBASE_STORAGE_ENGINE 安装INNODB
DWITH_ARCHIVE_STORAGE_ENGINE 存储引擎可用值
DWITH_BLACKHOLE_STORAGE_ENGINE 存储引擎可用值
DMYSQL_DATADIR 设置MySQL数据存放目录
DMYSQL_TCP_PORT MySQL监听端口

其中MyISAM,MERGE,MEMORYMERGE,CSV是默认编译生成的不需要明确指定

更多的参数含义可以参考MySQL官网文章
需要注意的是5.7版本的软件安装目录与之前版本不太一样,具体介绍

目录 目录内容
bin MySQL服务端,客户端可执行文件
docs MySQL文档格式信息
include 包含的头文件
lib 库文件
man MySQL手册信息
mysql-test MySQL测试文件
share 支持文件
support-files MySQL启动脚本

详细内容可以参考MySQL官网文章

MySQL服务开启与配置

创建MySQL配置文件

  1. cd /usr/local/mysql
  2. vim my.cnf

将如下配置项写入my.cnf中(这里需要说明的是MySQL 5.7.18之后安装目录中就没有my.cnf)

  1. [mysqld]
  2. basedir=/usr/local/mysql
  3. datadir=/var/mysql/data
  4. port=3306
  5. socket=/usr/local/mysql/tmp/mysql.sock
  6. log-error=/usr/local/mysql/log/mysqld.log
  7. tmpdir=/tmp
  8. max_allowed_packet=32M
  9. #bind-address=127.0.0.1
  10. [client]
  11. port=3306
  12. socket=/usr/local/mysql/tmp/mysql.sock
  13. default-character-set=UTF8
  14. [manager]
  15. port=3306
  16. socket=/usr/local/mysql/tmp/mysql.sock
  17. pid-file=/usr/local/mysql/tmp/manager.pid
  18. default-mysqld-path=/usr/local/mysql/bin/mysqld.bin

开启MySQL和进行设置

  1. cd /usr/local/mysql/bin
  2. ./mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/var/mysql/data
  3. ./mysql_ssl_rsa_setup --datadir=/var/mysql/data
  4. ./mysqld_safe --user=mysql &
  5. cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
  6. /etc/init.d/mysqld start

需要注意的是在执行完./mysqld --initialize命令后会分配一个默认的密码在日志文件中,查看密码

  1. cat /usr/local/mysql/log/mysqld.log

详细选项参数说明可以使用如下命令

  1. ./mysqld --verbose --help

也可以查阅官网MySQL初始化文档

修改root密码

  1. mysql -u root -p密码
  2. mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpass');

如果需要设置MySQL了可以远程连接,可以进行如下设置

  1. mysql -uroot -p密码
  2. GRANT ALL ON *.* TO 'root'@'%' IDENTIFIED BY '密码' WITH GRANT OPTION;
  3. FLUSH PRIVILEGES;

这里值得注意的是配置远程登录首先需要注释掉配置文件中bind-address=ip这句话,才可以通过设置表权限进行远程登录的控制

设置环境变量方便使用SQL命令

  1. echo 'export PATH = $PATH:/usr/local/mysql/bin'

安装MySQL时遇到的错误

错误一

  1. make[2]: *** [unittest/gunit/CMakeFiles/merge_small_tests-t.dir/merge_small_tests.cc.o] Error 4
  2. make[1]: *** [unittest/gunit/CMakeFiles/merge_small_tests-t.dir/all] Error 2
  3. make: *** [all] Error 2

错误产生原因:swap分区不够

解决办法:

  1. [root@VM_6_37_centos ~]# dd if=/dev/zero of=/swapfile bs=1k count=2048000
  2. [root@VM_6_37_centos ~]# mkswap /swapfile
  3. [root@VM_6_37_centos ~]# swapon /swapfile[root@web_1 ~]# dd if=/dev/zero of=/swapfile bs=1k count=2048000
  4. [root@VM_6_37_centos ~]# mkswap /swapfile
  5. [root@VM_6_37_centos ~]# swapon /swapfile

错误二

  1. Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

错误产生原因:没有启动MySQL

解决办法:开启MySQL

  1. /usr/local/mysql/support-files/mysql.server start

错误三

  1. mysql: [Warning] Using a password on the command line interface can be insecure.
  2. ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

错误产生原因:密码错误

解决办法:查看日志文件,找到初始密码进行登录(日志文件目录在配置文件制定)

  1. cat /usr/local/mysql/log/mysqld.log

这里需要说明,安装启动MySQL遇到错误提示可能是相同的,我们可以根据自己的情况进行查明

Nginx编译安装

前提工作

创建用户及用户组

  1. groupadd www
  2. useradd -g www www -s /bin/bash/false

编译安装Ngnix

  1. cd /usr/src/
  2. tar -zxvf nginx-1.12.0.tar.gz
  3. cd nginx-1.12.0/
  4. ./configure --prefix=/usr/local/ngnix --user=www --group=www --with-http_stub_status_module --with_http_ssl_module --with-http_gzip_static_module --with-threads
  5. make && make install

./configure选项参数可以通过./configure --help查看
可以查阅官网文档

创建网站根目录及Ngnix配置

创建网站根目录并修改用户权限

  1. mkdir /var/www
  2. chown www:www /var/www

进入配置文件目录cd /usr/local/ngnix/conf/

  1. cd /usr/local/ngnix/conf/ngnix.conf
  2. vim ngnix.conf

将网站的根目录修改为/var/www

  1. vim /usr/local/ngnix/cong/ngnix.conf
  2. #在http的server中修改localtion的root属性
  3. http{
  4. server{
  5. localtion / {
  6. root /var/www
  7. }
  8. }
  9. }

更多的配置项可以参考官网文档

启动Ngnix

启动Ngnix

  1. /usr/local/ngnix/sbin/ngnix

需要说明的是Ngnix有相应的选项参数,可以通过ngnix -h进行查看

  1. Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]
  2. Options:
  3. -?,-h : this help
  4. -v : show version and exit
  5. -V : show version and configure options then exit
  6. -t : test configuration and exit
  7. -T : test configuration, dump it and exit
  8. -q : suppress non-error messages during configuration testing
  9. -s signal : send signal to a master process: stop, quit, reopen, reload
  10. -p prefix : set prefix path (default: /usr/local/ngnix/)
  11. -c filename : set configuration file (default: conf/nginx.conf)
  12. -g directives : set global directives out of configuration file

值得注意的是-s选项的参数,有如下四种

名称 含义
stop 快速关掉ngnix进程
quit 优雅关掉ngnix进程即等待其它请求结束后在关闭ngnix进程
reload 重新加载配置文件
reopen 重新打开日志文件

到此ngnix安装结束

PHP7.1.4的编译安装

编译安装

  1. cd /usr/src
  2. tar -zxvf php-7.1.4.tar.gz
  3. cd php-7.1.4
  4. ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysqli=/usr/local/mysql/bin/mysql_config --with-mysql-sock=/tmp/mysql.sock --with-pdo-mysql=/usr/local/mysql --with-iconv --enable-libxml --enable-xml --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --enable-opcache --enable-mbregex --enable-fpm --enable-mbstring --enable-ftp --enable-gd-native-ttf --with-openssl --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-gettext --enable-session --with-mcrypt --with-curl --enable-ctype --enable-mysqlnd
  5. make && make install

值得注意的是:./configure后面的选项可以通过./configure --help命令进行详细查看,需要注意的是php-fpm模块时必须安装的

PHP相关配置

从编译后的源码包中复制配置文件到安装目录

  1. cp /usr/src/php-7.1.4/php.ini-production /usr/local/php/etc/php.ini

编辑php-fpm的配置文件

  1. cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
  2. vim /usr/local/php/etc/php-fpm.conf
  3. #取消前面的分号
  4. pid = run/php-fpm.pid
  5. cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
  6. #改变用户组及用户
  7. user = www
  8. group = www

php-fpm启动脚本加/etc/init.d/中并设置执行权限

  1. cp /usr/src/php-7.1.4/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
  2. chmod 755 /etc/init.d/php-fpm

启动php-fpm

  1. /etc/init.d/php-fpm start

配置Ngnix支持PHP

编辑Ngnix配置文件ngnix.conf取消如下显示内容的注释

  1. vim /usr/local/ngnix/conf/ngnix.conf
  2. http{
  3. server{
  4. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  5. location ~ \.php$ {
  6. root /var/www;#修改网站根目录
  7. fastcgi_pass 127.0.0.1:9000;
  8. fastcgi_index index.php;
  9. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;#注意这行需要修改
  10. include fastcgi_params;
  11. }
  12. }
  13. }

重启php-fpm及ngnix

  1. /etc/init.d/php-fpm restart
  2. /usr/local/ngnix/sbin/ngnix -s reload

到此安装完毕

安装测试

在浏览器中输入相应的IP地址
结果显示

参考文章及文档

MYSQL官方文档
NGNIX官方文档
PHP官方文档
LNMP平台搭建

其他

NINGX启动脚本

如果觉得每次到ngnixsbin目录下进行服务器管理操作比较麻烦可以将如下的ngnix启动脚本加到/etc/init.d/目录下

脚本代码

  1. #!/bin/sh
  2. #
  3. # nginx - this script starts and stops the nginx daemin
  4. #
  5. # chkconfig: - 85 15
  6. # description: Nginx is an HTTP(S) server, HTTP(S) reverse \
  7. # proxy and IMAP/POP3 proxy server
  8. # processname: nginx
  9. # config: /usr/local/nginx/conf/nginx.conf
  10. # pidfile: /usr/local/nginx/logs/nginx.pid
  11. # Source function library.
  12. . /etc/rc.d/init.d/functions
  13. # Source networking configuration.
  14. . /etc/sysconfig/network
  15. # Check that networking is up.
  16. [ "$NETWORKING" = "no" ] && exit 0
  17. nginx="/usr/local/nginx/sbin/nginx"
  18. prog=$(basename $nginx)
  19. NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
  20. lockfile=/var/lock/subsys/nginx
  21. start() {
  22. [ -x $nginx ] || exit 5
  23. [ -f $NGINX_CONF_FILE ] || exit 6
  24. echo -n $"Starting $prog: "
  25. daemon $nginx -c $NGINX_CONF_FILE
  26. retval=$?
  27. echo
  28. [ $retval -eq 0 ] && touch $lockfile
  29. return $retval
  30. }
  31. stop() {
  32. echo -n $"Stopping $prog: "
  33. killproc $prog -QUIT
  34. retval=$?
  35. echo
  36. [ $retval -eq 0 ] && rm -f $lockfile
  37. return $retval
  38. }
  39. restart() {
  40. configtest || return $?
  41. stop
  42. start
  43. }
  44. reload() {
  45. configtest || return $?
  46. echo -n $"Reloading $prog: "
  47. killproc $nginx -HUP
  48. RETVAL=$?
  49. echo
  50. }
  51. force_reload() {
  52. restart
  53. }
  54. configtest() {
  55. $nginx -t -c $NGINX_CONF_FILE
  56. }
  57. rh_status() {
  58. status $prog
  59. }
  60. rh_status_q() {
  61. rh_status >/dev/null 2>&1
  62. }
  63. case "$1" in
  64. start)
  65. rh_status_q && exit 0
  66. $1
  67. ;;
  68. stop)
  69. rh_status_q || exit 0
  70. $1
  71. ;;
  72. restart|configtest)
  73. $1
  74. ;;
  75. reload)
  76. rh_status_q || exit 7
  77. $1
  78. ;;
  79. force-reload)
  80. force_reload
  81. ;;
  82. status)
  83. rh_status
  84. ;;
  85. condrestart|try-restart)
  86. rh_status_q || exit 0
  87. ;;
  88. *)
  89. echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
  90. exit 2
  91. esac

具体操作

  1. vim /etc/init.d/ngnix
  2. #将上述代码复制进去,之后进行权限设置
  3. chmod 755 /etc/init.d/ngnix
posted @ 2017-04-29 21:03  vacuity  阅读(314)  评论(0编辑  收藏  举报