Centos6.4搭建LNMP
环境准备:centos6.4
软件包:nginx-1.10.2.tar.gz+MySQL-5.6.17.tar.gz+php-5.5.38.tar.xz
第一章 Nginx编译安装
1.1 创建nginx用户和组
[root@i-bofspu7g ~]# groupadd nginx
[root@i-bofspu7g ~]# useradd -r -g nginx nginx
[root@i-bofspu7g ~]# id nginx
uid=498(nginx) gid=500(nginx) groups=500(nginx)
1.2 下载源码包解压编译
[root@i-bofspu7g ~]# wget http://nginx.org/download/nginx-1.10.2.tar.gz
--2017-11-21 14:18:38-- http://nginx.org/download/nginx-1.10.2.tar.gz
Resolving nginx.org... 95.211.80.227, 206.251.255.63, 2606:7100:1:69::3f, ...
Connecting to nginx.org|95.211.80.227|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 910812 (889K) [application/octet-stream]
Saving to: “nginx-1.10.2.tar.gz”
100%[===================================================================================================================================================>] 910,812 405K/s in 2.2s
2017-11-21 14:18:41 (405 KB/s) - “nginx-1.10.2.tar.gz” saved [910812/910812]
[root@i-bofspu7g ~]# tar axf nginx-1.10.2.tar.gz -C /usr/local/
[root@i-bofspu7g ~]# yum groupinstall "Development tools" -y
[root@i-bofspu7g ~]# yum -y install gcc wget gcc-c++ automake autoconf libtool libxml2-devel libxslt-devel perl-devel perl-ExtUtils-Embed pcre-devel openssl-devel
[root@i-bofspu7g nginx-1.10.2]# ./configure --prefix=/usr/local/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --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 --user=nginx --group=nginx --with-pcre --with-http_v2_module --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-mail --with-mail_ssl_module --with-file-aio --with-ipv6 --with-http_v2_module --with-threads --with-stream --with-stream_ssl_module
[root@i-bofspu7g nginx-1.10.2]# make && make install
[root@i-bofspu7g nginx-1.10.2]# mkdir -pv /var/tmp/nginx/client
3、nginx启动脚本
1. # vim /etc/init.d/nginx
2. #!/bin/sh
3. #
4. # nginx - this script starts and stops the nginx daemon
5. #
6. # chkconfig: - 85 15
7. # description: Nginx is an HTTP(S) server, HTTP(S) reverse \
8. # proxy and IMAP/POP3 proxy server
9. # processname: nginx
10. # config: /etc/nginx/nginx.conf
11. # config: /etc/sysconfig/nginx
12. # pidfile: /var/run/nginx.pid
13.
14. # Source function library.
15. . /etc/rc.d/init.d/functions
16.
17. # Source networking configuration.
18. . /etc/sysconfig/network
19.
20. # Check that networking is up.
21. [ "$NETWORKING" = "no" ] && exit0
22.
23. nginx="/usr/sbin/nginx"
24. prog=$(basename $nginx)
25.
26. NGINX_CONF_FILE="/etc/nginx/nginx.conf"
27.
28. [ -f/etc/sysconfig/nginx] && . /etc/sysconfig/nginx
29.
30. lockfile=/var/lock/subsys/nginx
31.
32. start() {
33. [ -x $nginx] || exit5
34. [ -f $NGINX_CONF_FILE] || exit6
35. echo -n $"Starting $prog: "
36. daemon $nginx -c $NGINX_CONF_FILE
37. retval=$?
38. echo
39. [ $retval-eq0 ] && touch $lockfile
40. return $retval
41. }
42.
43. stop() {
44. echo -n $"Stopping $prog: "
45. killproc $prog -QUIT
46. retval=$?
47. echo
48. [ $retval-eq0 ] && rm-f $lockfile
49. return $retval
50. killall -9 nginx
51. }
52.
53. restart() {
54. configtest || return $?
55. stop
56. sleep 1
57. start
58. }
59.
60. reload() {
61. configtest || return $?
62. echo -n $"Reloading $prog: "
63. killproc $nginx -HUP
64. RETVAL=$?
65. echo
66. }
67.
68. force_reload() {
69. restart
70. }
71.
72. configtest() {
73. $nginx -t -c $NGINX_CONF_FILE
74. }
75.
76. rh_status() {
77. status $prog
78. }
79.
80. rh_status_q() {
81. rh_status >/dev/null2>&1
82. }
83.
84. case "$1" in
85. start)
86. rh_status_q && exit 0
87. $1
88. ;;
89. stop)
90. rh_status_q || exit 0
91. $1
92. ;;
93. restart|configtest)
94. $1
95. ;;
96. reload)
97. rh_status_q || exit 7
98. $1
99. ;;
100. force-reload)
101. force_reload
102. ;;
103. status)
104. rh_status
105. ;;
106. condrestart|try-restart)
107. rh_status_q || exit 0
108. ;;
109. *)
110. echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
111. exit 2
112. esac
[root@i-bofspu7g nginx-1.10.2]# cp nginx /etc/init.d/nginx
[root@i-bofspu7g nginx-1.10.2]# chmod +x /etc/init.d/nginx
[root@i-bofspu7g nginx-1.10.2]# chkconfig --add nginx
[root@i-bofspu7g nginx-1.10.2]# chkconfig nginx on
[root@i-bofspu7g nginx-1.10.2]# service nginx start
[root@i-bofspu7g nginx-1.10.2]# ss -tnulp | grep nginx
tcp LISTEN 0 128 *:80 *:* users:(("nginx",4862,6),("nginx",4863,6))
[root@i-bofspu7g nginx-1.10.2]# ps -ef | grep nginx
root 4862 1 0 14:25 ? 00:00:00 nginx: master process nginx
nginx 4863 4862 0 14:25 ? 00:00:00 nginx: worker process
root 4961 1286 0 14:32 pts/0 00:00:00 grep nginx
至此nginx配置完成。
第二章 MySQL编译安装
2.1 准备编译环境
[root@i-bofspu7g ~]# yum groupinstall "Server Platform Development" "Development tools" -y
[root@i-bofspu7g ~]# yum install cmake -y
2.2 主备MySQL数据目录
[root@i-bofspu7g ~]# mkdir /u01/data -pv
[root@i-bofspu7g ~]# groupadd -r mysql
[root@i-bofspu7g ~]# useradd -r -g mysql -s /sbin/nologin mysql
[root@i-bofspu7g ~]# id mysql
uid=497(mysql) gid=499(mysql) groups=499(mysql)
2.3 更换数据目录的主属组
[root@i-bofspu7g ~]# chown -R mysql:mysql /u01/data
2.4 编译安装MySQL
[root@i-bofspu7g mysql-5.6.17]# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/u01/data -DSYSCONFDIR=/etc -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DWITH_SSL=system -DWITH_ZLIB=system -DWITH_LIBWRAP=0 -DMYSQL_TCP_PORT=3306 -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci
[root@i-bofspu7g mysql-5.6.17]# make && make install
2.5 修改安装目录的主属组
[root@i-bofspu7g mysql-5.6.17]# chown -R mysql:mysql /usr/local/mysql/
2.5 初始化数据库
[root@i-bofspu7g mysql-5.6.17]# /usr/local/mysql/scripts/mysql_install_db --userdir=mysql --datadir=/u01/data/ --basedir=/usr/local/mysql
2.6拷贝配置文件和启动脚本
[root@i-bofspu7g mysql-5.6.17]# mv /etc/my.cnf /etc/my.cnf.bak
[root@i-bofspu7g mysql-5.6.17]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@i-bofspu7g mysql-5.6.17]# chmod +x /etc/init.d/mysqld
[root@i-bofspu7g mysql-5.6.17]# cp support-files/my-default.cnf /etc/my.cnf
注:在CentOS 6.4版操作系统的最小安装完成后,在/etc目录下会存在一个my.cnf,需要将此文件更名为其他的名字,如:/etc/my.cnf.bak,否则,该文件会干扰源码安装的MySQL的正确配置,造成无法启动。
2.7 设置开机启动
[root@i-bofspu7g mysql-5.6.17]# chkconfig mysqld on
[root@i-bofspu7g mysql-5.6.17]# chkconfig --add mysqld
2.8 修改my.cnf配置文件
[root@i-bofspu7g mysql-5.6.17]# cat /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/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 = /u01/data
port = 3306
# server_id = .....
# socket = .....
# 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
2.9 设置path环境变量
[root@i-bofspu7g mysql-5.6.17]# echo "export PATH=$PATH:/usr/local/mysql/bin" > /etc/profile
[root@i-bofspu7g mysql-5.6.17]# source /etc/profile
2.10 启动服务
[root@i-bofspu7g mysql-5.6.17]# /etc/init.d/mysqld start
Starting MySQL. SUCCESS!
[root@i-bofspu7g mysql-5.6.17]# ss -tnulp | grep mysql
[root@i-bofspu7g mysql-5.6.17]# ps -ef |grep mysql
[root@i-bofspu7g mysql-5.6.17]# mysql
至此MySLQ安装完成。
第三章 php-fpm编译安装
3.1 安装依赖包
[root@i-bofspu7g ~]# yum install libmcrypt libmcrypt-devel mhash mhash-devel libxml2 libxml2-devel bzip2 bzip2-devel
3.2 编译安装php-fpm
[root@i-bofspu7g ~]# tar axf php-5.5.38.tar.xz -C /usr/local/
[root@i-bofspu7g ~]# cd /usr/local/php-5.5.38/
[root@i-bofspu7g php-5.5.38]# make && make install
3.3 添加php和php配置文件
[root@i-bofspu7g php-5.5.38]# cp php.ini-production /etc/php.ini
[root@i-bofspu7g php-5.5.38]# cd /usr/local/php/etc/
[root@i-bofspu7g etc]# cp php-fpm.conf.default php-fpm.conf
[root@i-bofspu7g etc]# sed -i 's#;pid = run/php-fpm.pid#pid = /usr/local/php/var/run/php-fpm.pid#' php-fpm.conf
3.4 添加php启动脚本
[root@i-bofspu7g etc]# cp /usr/local/php-5.5.38/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@i-bofspu7g etc]# chmod +x /etc/init.d/php-fpm
3.5 设置php-fpm开机启动
[root@i-bofspu7g etc]# chkconfig --add php-fpm
[root@i-bofspu7g etc]# chkconfig --list php-fpm
[root@i-bofspu7g etc]# chkconfig php-fpm on
3.6 启动php-fpm服务
[root@i-bofspu7g etc]# /etc/init.d/php-fpm start
Starting php-fpm done
3.7 添加nginx对fastcgi支持
[root@i-bofspu7g etc]# mv /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
[root@i-bofspu7g etc]# cp /etc/nginx/nginx.conf.default /etc/nginx/nginx.conf
编辑/etc/nginx/nginx.conf,在所支持的主页面格式中添加php格式的主页,类似如下:
location / {
root /usr/local/nginx/html;
index index.php index.html index.htm;
}
取消以下内容前面的注释:
location ~ \.php$ {
root /usr/local/nginx/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html/$fastcgi_script_name;
include fastcgi_params;
}
重新载入nginx的配置文件。
# service nginx reload
在/usr/local/nginx/html/新建index.php的测试页面,内容如下。
# cat index.php
<?php
$conn=mysql_connect('127.0.0.1','root','');
if ($conn){
echo "LNMP platform connect to mysql is successful!";
}else{
echo "LNMP platform connect to mysql is failed!";
}
phpinfo();
?>
小结:至此php就安装完成,整个LNMP就配置成功了。