lnmp是常见的web服务器环境,本文选用比较高版本的nginx和mysq,在内存只有1G的阿里云服务器上配置web开发环境,并给出配置过程中相关错误的解决方法。
一、准备相关软件包
Nginx1.10.1
PHP5.5.36
MySQL5.6.32
二、配置lemp
1.配置nginx
1>解决依赖关系
yum install pcre-devel
yum install gcc
yum install zlib-devel
yum install gcc-c++
2>创建nginx用户
groupadd -r nginx
useradd -g nginx -s /sbin/nologin nginx #创建系统用户nginx 属组nginx
3>编译安装
./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/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--user=nginx \
--group=nginx \
--with-http_flv_module \
--with-http_stub_status_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 \
--with-http_perl_module \
--with-http_realip_module
make
make install
错误解决:./configure: error: perl module ExtUtils::Embed is required
[root@www nginx-1.10.1]# yum install perl-ExtUtils-Embed
4>添加系统启动脚本
vim /etc/init.d/nginx
内容如下:
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
}
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
sleep 1
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
# chmod +x /etc/rc.d/init.d/nginx
添加至服务管理列表,并让其开机自动启动:
# chkconfig --add nginx
# chkconfig nginx on
而后就可以启动服务并测试了:
# service nginx start
2.安装MySQL
这里我采用了编译安装和二进制安装两种方式,编译安装在make时总是报错,用了网上的解决方案也不行,也许是其他问题,为了节省时间,本文采用二进制方式安装。编译安装的步骤也给出来了,但仅供参考。
1>创建数据库用户
groupadd mysql
useradd -g mysql -s /sbin/nologin mysql #非登录用户
2>建立mysql安装目录及数据存放目录
# mkdir /usr/local/mysql
# mkdir /data/mysql/data
chown mysql:mysql -R /data/mysql/data
3>解决依赖关系
yum install cmake
yum install ncurses-devel
yum install bison
yum install git openssl-devel
4>二进制安装
wget http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.32-linux-glibc2.5-x86_64.tar.gz
tar -zxvf mysql-5.6.32-linux-glibc2.5-x86_64.tar.gz
cd mysql-5.6.32-linux-glibc2.5-x86_64
cp -r ./* /usr/local/mysql/
编译安装参考步骤:
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/data/mysql/ \
-DMYSQL_TCP_PORT=3306 \
-DMYSQL_UNIX_ADDR=/tmp/mysqld.sock \
-DMYSQL_USER=mysql \
-DEXTRA_CHARSETS=all \
-DENABLED_LOCAL_INFILE=ON \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \
-DWITH_FAST_MUTEXES=1 \
-DWITH_ZLIB=bundled \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_READLINE=1 \
-DWITH_EMBEDDED_SERVER=1 \
-DWITH_DEBUG=0
make
make install
注:如果make到63%左右出现错误:
make[2]: * [storage/perfschema/unittest/pfs_connect_attr-t] Error 1
make[1]: * [storage/perfschema/unittest/CMakeFiles/pfs_connect_attr-t.dir/all] Error 2
则:cmake去掉-DWITH_PARTITION_STORAGE_ENGINE=1参数重新编译
或者-DWITHOUT_PARTITION_STORAGE_ENGINE=0 如下: make clean rm CMakeCache.txt cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \ -DMYSQL_DATADIR=/data/mysql/ \ -DMYSQL_TCP_PORT=3306 \ -DMYSQL_UNIX_ADDR=/tmp/mysqld.sock \ -DMYSQL_USER=mysql \ -DEXTRA_CHARSETS=all \ -DWITH_READLINE=1 \ -DWITH_SSL=system \ -DWITH_EMBEDDED_SERVER=1 \ -DENABLED_LOCAL_INFILE=1 \ -DWITH_INNOBASE_STORAGE_ENGINE=1 \ -DWITHOUT_PARTITION_STORAGE_ENGINE=0
cp support-files/my-medium.cnf /etc/my.cnf
5>数据库初始化
chmod 755 scripts/mysql_install_db
scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/data/mysql/data/
6>添加系统启动脚本
cp support-files/mysql.server /etc/init.d/mysql
chmod +x /etc/init.d/mysql
chkconfig --add mysql
chkconfig mysql on
修改mysql否则启动会报错:
Starting MySQL..... ERROR! The server quit without updating PID file.
错误日志:/mysql-bin.index' not found (Errcode: 13
原因分析:mysql的数据路径为默认路径,不是编译时指定的路径。
解决方法:vim /etc/init.d/mysql
更改:datadir=/data/mysql/data
或者 vim /etc/my.cnf
在[mysqld]部分添加:datadir=/data/mysql/data
service mysql start
7>添加环境
echo 'PATH=/usr/local/mysql/bin:$PATH export PATH' >>/etc/profile
source /etc/profile
8>删除匿名用户和设置用户密码
mysql -u root -p
mysql> select User,Host,Password from mysql.user;
mysql> delete from mysql.user where user='' && password='';
mysql> set password for 'root'@'localhost'=password('shch200818');
mysql> flush privileges;
#!/usr/bin/perl -w
注意:距离MySQL 5.6正式发布已经有比较长的时间了,目前Oracle官网上的最新GA版本MySQL server也为5.6。但在安装配置后却发现其内存占用居高不下,无论如何调整cache甚至禁用InnoDB都不能解决。由于VPS仅有1GB内存,在开启常用的Web服务之后,无力再为MySQL提供400MB以上的物理内存。
通过网络爬文,MySQL 5.6相比于前代GA版本性能提升显著,但默认缓存设置对于小型站点并不合理。通过修改my.ini文件中的performance_schema_max_table_instances参数,能够有效降低内存占用。
vim /etc/my.cnf
设置如下参数:
performance_schema_max_table_instances=400
table_definition_cache=400
table_open_cache=256
以上参数根据实际情况调整
保存配置重启mysql
3.PHP安装
1>解决依赖包
yum install gd-devel.x86_64 libxml2-devel bzip2-devel.x86_64 libcurl-devel
yum install libxml2-devel
yum install bzip2-devel.x86_64
yum install libcurl-devel
2>编译安装
tar -zxvf php-5.5.36.tar.gz
cd php-5.5.36
./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl --enable-fpm --enable-sockets --enable-sysvshm --enable-ftp --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-gd --with-zlib-dir --with-libxml-dir --enable-xml --with-mhash --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --with-curl --enable-opcache --disable-fileinfo
# make
# make test
# make intall
make: *** [ext/fileinfo/libmagic/apprentice.lo] Error 1
解决办法
这是由于内存小于1G所导致.
在./configure加上选项:
1
--disable-fileinfo
3>创建初始化文件
cp php.ini-production /etc/php.ini
4>创建系统启动脚本
cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm
chmod +x /etc/rc.d/init.d/php-fpm
chkconfig php-fpm on
5>创建配置文件
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
修改 vim /usr/local/php/etc/php-fpm.conf
配置fpm的相关选项为你所需要的值,并启用pid文件(如下最后一行):
pm.max_children = 150
pm.start_servers = 8
pm.min_spare_servers = 5
pm.max_spare_servers = 10
pid = run/php-fpm.pid
service php-fpm start #重新启动
netstat -tnlp #查看是否有127.0.0.1:9000
三、整合php-fpm和nginx
1、编辑/etc/nginx/nginx.conf,启用如下选项:
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param script_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params;
}
2.编辑/etc/nginx/fastcgi_params,将其内容更改为如下内容:
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param script_FILENAME $document_root$fastcgi_script_name; #添加这一行
fastcgi_param script_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
3.在所支持的主页面格式中添加php格式的主页,类似如下:
location / {
root html;
index index.php index.html index.htm;
}
而后重新载入nginx的配置文件:
# service nginx reload
4.在/usr/html新建index.php的测试页面,测试php是否能正常工作:
脚本:
<?php
phpinfo();
?>
接着就可以通过浏览器访问此测试页面了。
|