CentOS6.x编译安装LNMP(Linux+Nginx+Mysql+PHP)

一、准备依赖包及扩展组件

1
yum install -y gcc gcc-c++ make curl-devel kernel-headers glibc glibc-headers zlib zlib-devel openssl openssl-devel pcre-devel perl compat* php-common ncurses-devel libtool* libxml2 libxml2-devel

二、安装nginx

1
2
3
4
5
6
7
8
9
groupadd nginx
useradd -g nginx -s /sbin/nologin nginx
tar zxvf nginx-1.4.2.tar.gz
cd nginx-1.4.2
 ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx
--with-http_ssl_module --with-http_gzip_static_module
--with-http_stub_status_module
make && make install
/usr/local/nginx/sbin/nginx #启动nginx

Nginx启动、停止、重启等SysV管理脚本,方便使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
vi /etc/init.d/nginx
#!/bin/bash
# chkconfig: 345 99 20
# description: Nginx service control script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$PROG
echo "Nginx service start success."
;;
stop)
kill -s QUIT $(cat $PIDF)
echo "Nginx service stop success."
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUP $(cat $PIDF)
echo"reload Nginx config success."
;;
*)
echo "Usage: $0 {start|stop|restart|reload}"
exit 1
esac
1
2
3
4
chmod +x /etc/init.d/nginx
service nginx start
chkconfig --add nginx
chkconfig nginx on

测试:http://127.0.0.1 #出现欢迎nginx页面成功

三、安装mysql

1、创建mysql组和用户

1
2
groupadd mysql
useradd –g mysql -s /sbin/nologin mysql

2、安装mysql

1
2
3
4
5
6
tar zxvf mysql-5.5.30.tar.gz
cd mysql-5.5.30
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DSYSCONFDIR=/usr/local/mysql/etc \
-DMYSQL_DATADIR=/usr/local/mysql/data
make && make install

3、配置mysql 

1
2
3
4
5
6
7
8
9
10
11
12
13
/usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/ --user=mysql #初始化数据库
cp support-files/my-medium.cnf /usr/local/mysql/etc/my.cnf
cp support-files/mysql.server /etc/init.d/mysqld
echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile #添加执行命令环境变量
source /etc/profile #生效环境变量
chmod +x /etc/init.d/mysqld
chown -R root.mysql /usr/local/mysql/
chown -R mysql:mysql mysql/data/ #赋予数据存放目录权限
mysqld_safe --user=mysql& #启动mysql
rm -rf /etc/my.cnf   #删除mysql以前的配置文件,否则有时会起不来
service mysqld start
mysqladmin -u root password '123456'#设置root登录密码
chkconfig mysqld on

四、安装php和配置php-fpm

1、安装gd库

1
yum install –y gd php-gd freetype freetype-devel libpng libpng-devel libjpeg*

2、安装php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
tar zxvf php-5.4.tar.gz
./configure --prefix=/usr/local/php \
-with-config-file-path=/usr/local/php/etc \
-with-mysql=/usr/local/mysql \
-with-mysqli=/usr/local/mysql/bin/mysql_config \
-with-mysql-sock=/tmp/mysql.sock \
-with-pdo-mysql=/usr/local/mysql \
-with-gd -with-zlib -with-iconv -enable-zip -enable-pdo \
-enable-xml -with-openssl -with-curl -enable-bcmath \
-enable-ftp -enable-xml -with-openssl -with-curl \
-enable-bcmath -enable-ftp -enable-mbstring -enable-fpm \
-with-fpm-user=nginx -with-fpm-group=nginx -enable-shmop \
-enable-sysvsem -enable-mbregex -enable-gd-native-ttf \
-enable-pcntl -enable-sockets -with-xmlrpc -enable-soap \
-without-pear -with-gettext -enable-session
make && make install

3、配置php

1
2
3
4
cp php.ini-production /usr/local/php/etc/php.ini
vi /usr/local/php/etc/php.ini
date.timezone = Asia/Shanghai #设置时区
expose_php = OFF #禁止显示版本信息

4、置php-fpm

1
2
3
4
5
6
7
8
9
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
vi /usr/local/php/etc/php-fpm.conf
user = nginx
group = nginx
pid = run/php-fpm.pid
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/rc.d/init.d/php-fpm
service php-fpm start
chkconfig php-fpm on

五、nginx和php整合

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
vi /usr/local/nginx/conf/nginx.conf #修改并去掉以下注释
user nginx nginx;
error_log logs/error.log;
worker_processes 4; #默认创建子进程个数
events {
 worker_connections 1024; #一个子进程最大连接数
}
pid logs/nginx.pid;
...................
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; #SCRIPT_FILENAME后面写网站根目录
    include fastcgi_params;
 }
...................
1
2
3
4
/usr/local/nginx/sbin/nginx -t #测试语法是否正确
service nginx restart
chown -R nginx /usr/local/nginx/html
chmod 744 -R /usr/local/nginx/html

六、测试

1
2
3
4
5
6
service nginx restart
service php-fpm restart
vi index.php
<?php
phpinfo();
?>

http://127.0.0.1,可以看到相关的配置信息!

上一篇:SAP HANA中创建与时间相关的数据及Time Attribute View(Fiscal)


下一篇:linux命令:编译安装软件包(举例安装tengine nginx)