编译安装LNMP
1. MySQL的编译安装:
1. 增加一个mysql用户:-M就不生成家目录
# useradd -s /sbin/nologin -M mysql
2. 在data目录下面建立一个mysql目录,并修改权限:-p创建级联目录,-R继承权限
# mkdir -p /data/mysql # chown -R mysql:mysql /data/mysql
3. 下载MySQL安装包:下载地址选择的是搜狐镜像:mirrors.sohu.com,需要其余版本的可以在里面自己找。源码包的大小目前不超过100M(2016年)。
# cd /usr/local/src # wget http://mirrors.sohu.com/mysql/MySQL-5.7/mysql-5.7.11.tar.gz
4. 下载完成后,将mysql解压并进行cmake和后续操作:
# tar zxvf mysql-5.7.11.tar.gz # cd mysql-5.7.11 # cmake \ -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \ -DMYSQL_DATADIR=/data/mysql/\ -DSYSCONFDIR=/etc \ -DWITH_MYISAM_STORAGE_ENGINE=1 \ -DWITH_INNOBASE_STORAGE_ENGINE=1 \ -DWITH_MEMORY_STORAGE_ENGINE=1 \ -DWITH_READLINE=1 \ -DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock \ -DMYSQL_TCP_PORT=3306 \ -DENABLED_LOCAL_INFILE=1 \ -DWITH_PARTITION_STORAGE_ENGINE=1 \ -DEXTRA_CHARSETS=all \ -DDEFAULT_CHARSET=utf8 \ -DDEFAULT_COLLATION=utf8_general_ci # make && make install
因为MySQL本身很大,所以,make的时间会非常长。
5. 下面进行初始化:
./scripts/mysql_install_db --basedir=/usr/local/mysql/ --datadir=/data/mysql/ --user=mysql
出现错误: bash: ./scripts/mysql_install_db: 权限不够;查询./scripts/mysql_install_db的权限,发现没有x:
# ll ./scripts/mysql_install_db
-rw-r--r-- 1 mysql mysql 33853 12月 3 18:24 ./scripts/mysql_install_db
添加x权限,重新初始化:
# chmod a+x ./scripts/mysql_install_db # ./scripts/mysql_install_db --basedir=/usr/local/mysql/ --datadir=/data/mysql/ --user=mysql
6. 初始化成功,出现两个 OK,说明初始化成功。拷贝配置文件到相应位置:
# cp my.cnf /etc/my.cnf ## 覆盖原来的my.cnf # cd support-files/ # cp mysql.server /etc/init.d/mysqld # chmod 755 !$ # /etc/init.d/mysqld start # ps aux |grep mysqld
启动MySQL服务以后,在进程里面可以看到mysqld。
2. php的编译安装
针对Nginx的php安装和针对apache的php安装是有区别的。区别在于,Nginx中的php是以fastcgi的方式结合nginx的。可以理解为nginx代理了php的fastcgi,而apache是把php作为自己的模块来调用的。
PHP官方下载地址: http://www.php.net/downloads.php,以5.4版本为例:
php和nginx的安装没有顺序,下面就先安装php。
1. 下载和建立用户
# cd /usr/local/src # wget http://au1.php.net/distributions/php-5.4.44.tar.bz2 # tar jxf php-5.4.44.tar.bz2 # useradd -s /sbin/nologin php-fpm
2. 配置编译参数:
# cd php-5.4.44 ./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-mysql-sock=/tmp/mysql.sock \ --with-libxml-dir \ --with-gd \ --with-jpeg-dir \ --with-png-dir \ --with-freetype-dir \ --with-iconv-dir \ --with-zlib-dir \ --with-mcrypt \ --enable-soap \ --enable-gd-native-ttf \ --enable-ftp \ --enable-mbstring \ --enable-exif \ --enable-zend-multibyte \ --disable-ipv6 \ --with-pear \ --with-curl \ --with-openssl
3. 安装
# make && make install
4.修改配置文件:
# cp php.ini-production /usr/local/php/etc/php.ini # > /usr/local/php/etc/php-fpm.conf
## 清空配置
# vim !$ ## 添加如下配置: [global] pid = /usr/local/php/var/run/php-fpm.pid error_log = /usr/local/php/var/log/php-fpm.log [www] listen = /tmp/php-fcgi.sock user = php-fpm group = php-fpm pm = dynamic pm.max_children = 50 pm.start_servers = 20 pm.min_spare_servers = 5 pm.max_spare_servers = 35 pm.max_requests = 500 rlimit_files = 1024 ## 保存配置文件后,检验配置是否正确的方法为: # /usr/local/php/sbin/php-fpm -t,可以设立一个alias。
5. 启动php
首先要拷贝一个启动脚本到/etc/init.d/下
# cp /usr/local/src/php-5.3.27/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm # chmod 755 /etc/init.d/php-fpm # chkconfig --add php-fpm # chkconfig php-fpm on ## 上面添加开机启动 # useradd -s /sbin/nologin -M php-fpm ## 在LNMP环境中,php是以一个服务方式来提供的,配置的时候,user = php-fpm; group = php-fpm就指定了这个用户,因此,需要建立一个php-fpm不可登录的账户来运行php-fpm服务。 # service php-fpm start # ps aux |grep php-fpm
如上,如果可以看到php-fpm的进程(20余个),说明配置成功。
3. Nginx的编译安装
1. 下载、解压Nginx
# cd /usr/local/src/ # wget http://nginx.org/download/nginx-1.8.0.tar.gz # tar zxvf nginx-1.8.0.tar.gz
2. 配置:
# cd nginx-1.8.0 # ./configure \ --prefix=/usr/local/nginx \ --with-http_realip_module \ --with-http_sub_module \ --with-http_gzip_static_module \ --with-http_stub_status_module \ --with-pcre
3. 编译、安装Nginx
# make && make install
4. 启动nginx,检查nginx是否启动:
# /usr/local/nginx/sbin/nginx # ps aux |grep nginx
【1】nginx启动脚本和配置文件
1. 编写启动脚本:
# vim /etc/init.d/nginx
将下面内容写入shell脚本:
#!/bin/bash
# chkconfig: - 30 21
# description: http service.
# Source Function Library
. /etc/init.d/functions
# Nginx Settings
NGINX_SBIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/logs/nginx.pid"
RETVAL=0
prog="Nginx"
start() {
echo -n $"Starting $prog: "
mkdir -p /dev/shm/nginx_temp
daemon $NGINX_SBIN -c $NGINX_CONF
RETVAL=$?
echo
return $RETVAL
}
stop() {
echo -n $"Stopping $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -TERM
rm -rf /dev/shm/nginx_temp
RETVAL=$?
echo
return $RETVAL
}
reload(){
echo -n $"Reloading $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -HUP
RETVAL=$?
echo
return $RETVAL
}
restart(){
stop
start
}
configtest(){
$NGINX_SBIN -c $NGINX_CONF -t
return 0
}
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
restart
;;
configtest)
configtest
;;
*)
echo $"Usage: $0 {start|stop|reload|restart|configtest}"
RETVAL=1
esac
exit $RETVAL
保存后,更改权限,添加到开机启动:
# chmod 755 /etc/init.d/nginx # chkconfig --add nginx # chkconfig nginx on
【2】Nginx的php解析功能添加
要支持php的解析,首先要修改配置文件,将PHP解析部分注释打开,修改默认的root目录路径,这样就可以在给定的路径下面对php进行解析了。
# 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 /usr/local/nginx/html$fastcgi_script_name;
include fastcgi_params;
}
注意,上面倒数第三行/usr/local/nginx/html是默认的访问路径,配置错误会显示:502 Bad Gateway!
# cd /usr/local/nginx/html # vim 1.php ## 写入如下内容: <?php phpinfo (); ?>
在浏览器中输入:192.168.220.11/1.php,如果能看到如下图般的php页面,说明配置正确:

4. 参数调优
【1】修改Nginx配置文件:
清空原来的配置文件:
# > /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;
## 如下打开vhosts目录的配置开关
include vhosts/*.conf;
}
检查配置有无错误的命令:
# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
显示如上内容,说明没有配置错误。另外,我们自定义的Nginx启动脚本也是可以检测语法错误的:
# /etc/init.d/nginx configtest
功能和上面的命令一样。
【2】关闭默认虚拟主机:
建立vhosts目录:
# cd /usr/local/nginx/conf
# mkdir vhosts
建立默认虚拟主机配置文件,禁止ip和非指定域名访问:
# cd vhosts/
# vim default.conf
## 写入如下内容:
server
{
listen 80 default_server;
server_name localhost;
index index.html index.htm index.php;
root /tmp/123;
deny all;
}
其中,/tmp/123需要创建,里面不用放文件,空目录即可。deny all表示一切非指定域名、或者ip直接访问的请求全部被禁止。
【3】配置一个可访问域名:
# vim aaa.conf
# 写入如下内容:
server { listen 80;
## 指定网址域名
server_name aaa.com;
index index.html index.htm index.php;
## 指定家目录,网页文件放在这个目录
root /usr/local/nginx/html;
location ~ \.php$ {
include fastcgi_params;
## 配置支持socket模式,视情况而定
fastcgi_pass unix:/tmp/www.sock;
fastcgi_index index.php;
## 指定家目录路径
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
}
}
system32\drivers\etc\hosts
注意:需要管理员权限才能编辑,写入如下配置:
192.168.220.11 aaa.com
如此在浏览器输入地址:
http://aaa.com/1.php
是否能得到和上面一样的php页面?能的话,说明配置成功。
curl工具的测试方法如下:
# curl -xlocalhost:80 aaa.com/1.php -I HTTP/1.1 200 OK Server: nginx/1.8.0 Date: Wed, 17 Feb 2016 14:47:47 GMT Content-Type: text/html Connection: keep-alive X-Powered-By: PHP/5.4.44
显示200 OK,表示访问成功。如此,自定义的网页域名就设置成功了。
【4】php-fpm.conf的参数优化:
清空原来的php-fpm.conf,写入新内容:
# > /usr/local/php/etc/php-fpm.conf # vim !$ ## 写入如下内容: [global] pid = /usr/local/php/var/run/php-fpm.pid error_log = /usr/local/php/var/log/php-fpm.log [www] listen = /tmp/www.sock user = php-fpm group = php-fpm listen.owner = nobody listen.group = nobody pm = dynamic pm.max_children = 50 pm.start_servers = 20 pm.min_spare_servers = 5 pm.max_spare_servers = 35 pm.max_requests = 500 rlimit_files = 10240
slowlog = /path/slow.log
request_slowlog_timeout = 1
php_admin_value[open_basedir]=/data/www/:/tmp/
其中,参数中的listen.owner = nobody,listen.group = nobody和的nginx的配置一致,否则,可能因为权限的问题导致无法访问目录,导致502 Bad Gateway! Slowlog的添加也有很重要的作用,可以方便在网页浏览变慢的时候,排除故障;open_basedir是开放的访问目录,用冒号进行扩展。

浙公网安备 33010602011771号