您的位置:首页 > 运维架构 > Linux

centos搭建lnmp环境

2018-08-14 08:57 260 查看
一、部署Nginx
1. pcre: 支持正则表达式,地址重写rewrite
# yum -y install pcre-devel

2. nginx
[root@tianyun ~]# useradd www -s /sbin/nologin
[root@tianyun ~]# tar xf nginx-1.8.0.tar.gz
[root@tianyun ~]# cd nginx-1.8.0
[root@tianyun nginx-1.8.0]# ./configure \
--user=www \
--group=www \
--prefix=/usr/local/nginx-1.8.0 \
--with-http_stub_status_module \
--with-http_sub_module \
--with-http_ssl_module \
--with-pcre
# make
# make install
# ln -s /usr/local/nginx-1.8.0 /usr/local/nginx

# tree /usr/local/nginx/
/usr/local/nginx/
|-- conf
| |-- fastcgi.conf
| |-- fastcgi.conf.default
| |-- fastcgi_params
| |-- fastcgi_params.default
| |-- koi-utf
| |-- koi-win
| |-- mime.types
| |-- mime.types.default
| |-- nginx.conf
| |-- nginx.conf.default
| |-- scgi_params
| |-- scgi_params.default
| |-- uwsgi_params
| |-- uwsgi_params.default
| `-- win-utf
|-- html
| |-- 50x.html
| `-- index.html
|-- logs
`-- sbin
`-- nginx

3. 启动
[root@tianyun ~]# /usr/local/nginx/sbin/nginx
[root@tianyun ~]# netstat -tnlp |grep :80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 10627/nginx

二、安装PHP
1. 以php-fpm的方式安装php
[root@tianyun ~]# yum -y install libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel \
libxml2 libxml2-devel libcurl libcurl-devel libxslt-devel openssl-devel
[root@tianyun ~]# tar xf php-5.5.7.tar.gz
[root@tianyun ~]# ./configure \
--prefix=/usr/local/php \
--with-curl \
--with-freetype-dir \
--with-gd \
--with-gettext \
--with-iconv-dir \
--with-jpeg-dir \
--with-kerberos \
--with-libdir=lib64 \
--with-libxml-dir \
--with-mysql \
--with-mysqli \
--with-openssl \
--with-pcre-regex \
--with-pdo-mysql \
--with-pdo-sqlite \
--with-pear \
--with-png-dir \
--with-xmlrpc \
--with-xsl \
--with-zlib \
--enable-fpm \
--enable-bcmath \
--enable-libxml \
--enable-inline-optimization \
--enable-gd-native-ttf \
--enable-mbregex \
--enable-mbstring \
--enable-opcache \
--enable-pcntl \
--enable-shmop \
--enable-soap \
--enable-sockets \
--enable-sysvsem \
--enable-xml \
--enable-zip
[root@tianyun php-5.5.7]# make && make install

2. php-fpm配置文件(影响php处理php程序的性能,例如php进程数、最大连接数配置等,运维人员关注)
[root@tianyun php-5.5.7]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

3. php置文件(影响php代码,例如允许客户端最大上传文件的大小,php所支持的扩展功能例如是否可以连接MySQL、Memcache,程序员关注)
[root@tianyun php-5.5.7]# cp php.ini-production /usr/local/php/lib/php.ini

4. 添加到init启动[可选]
[root@tianyun php-5.5.7]# cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm
[root@tianyun php-5.5.7]# chmod a+x /etc/rc.d/init.d/php-fpm
[root@tianyun php-5.5.7]# chkconfig --add php-fpm
[root@tianyun php-5.5.7]# chkconfig php-fpm on
[root@tianyun php-5.5.7]# service php-fpm start
Starting php-fpm done

[root@tianyun ~]# netstat -tnlp |grep :80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4408/nginx
[root@tianyun ~]# netstat -tnlp |grep :9000
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 1475/php-fpm

二、整合Nginx和PHP
1. Nginx启用Fastcgi
[root@tianyun php-5.5.7]# vim /usr/local/nginx/conf/nginx.conf
启用:
location / {
root html;
index index.php index.html index.htm;
}

方法一:使用TCP连接
去掉以下行的注释:
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

方法二:使用socket连接
[root@tianyun ~]# vim /usr/local/php/etc/php-fpm.conf
;listen = 127.0.0.1:9000
listen = /dev/shm/php-fpm.sock
[root@tianyun ~]# service php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm done
[root@tianyun ~]# ll /dev/shm/php-fpm.sock
srw-rw-rw-. 1 root root 0 Sep 18 04:55 /dev/shm/php-fpm.sock

[root@tianyun ~]# vim /usr/local/nginx/conf/nginx.conf
去掉以下行的注释:
location ~ \.php$ {
root html;
fastcgi_pass unix:/dev/shm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

2. 最终确认并重启
[root@tianyun ~]# /usr/local/nginx/sbin/nginx -s reload
[root@tianyun ~]# /etc/init.d/php-fpm restart
[root@tianyun ~]# netstat -tnlp |grep :80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 17947/nginx
[root@tianyun ~]# netstat -tnlp |grep :9000
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 17961/php-fpm

三、安装MySQL(略)
方案一:
[root@tianyun ~]# yum -y install mysql-server
[root@tianyun ~]# service mysqld start
[root@tianyun ~]# chkconfig mysqld on

方案二:
使用源码包编译安装(参考MySQL安装)

root@((none))> grant all on bbs.* to phptest@'192.168.1.186' identified by '123';
Query OK, 0 rows affected (0.00 sec)

root@((none))> flush privileges;
Query OK, 0 rows affected (0.00 sec)

五、php程序测试
1. 测试php文件能否执行
[root@tianyun ~]# cd /usr/local/nginx/html/
[root@tianyun html]# rm -rf *
[root@tianyun html]# vim index.php
<?php
phpinfo();
?>

2. 测试php连接MySQL是否正常
[root@tianyun html]# vim index2.php
<?php
$link=mysql_connect('192.168.1.1','phptest','123');
if ($link)
echo "Successfuly";
else
echo "Faile";
mysql_close();
?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: