LNMP环境搭建(CentOS7+Nginx1.10.1+MySQL5.6.31+PHP5.6.22)

零、准备工作

安装wget:yum install wget -y

安装gcc及g++:yum install gcc gcc-c++ -y

后续所有源代码都下载到/usr/local/src目录

防火墙更改配置及关闭selinux见另一篇文章《LAMP环境搭建》

一、安装Nginx

Nginx依赖pcre(重写rewrite)、zlib(网页gzip压缩)及openssl(加密传输)。

1、安装pcre

[root]wget http://pilotfiber.dl.sourceforge.net/project/pcre/pcre/8.38/pcre-8.38.tar.gz

[root]tar -xvzf pcre-8.38.tar.gz

[root]cd pcre-8.38

[root] ./configure --prefix=/usr/local/pcre

[root]make && make install

2、安装zlib

[root]wget http://zlib.net/zlib-1.2.8.tar.gz

[root]tar -xvzf zlib-1.2.8.tar.gz

[root]cd zlib-1.2.8

[root] ./configure --prefix=/usr/local/zlib

[root]make && make install

3、安装openssl

[root]wget http://www.openssl.org/source/openssl-1.0.2h.tar.gz

[root]tar -xvzf openssl-1.0.2h.tar.gz

[root]cd openssl-1.0.2h

[root] ./config --prefix=/usr/local/openssl

[root]make && make install

4、安装Nginx

为了安全起见,创建一个nginx账号专门用于运行nginx,当然为了简便直接用root账号运行的话(不推荐),就不需要创建nginx账号及将nginx相关文件开放权限给nginx账号。

[root]groupadd nginx

[root]useradd -g nginx nginx -s /bin/false#该账号只用于运行nginx及相关软件,不能登录

[root]wget http://nginx.org/download/nginx-1.10.1.tar.gz​

[root]tar -xvzf nginx-1.10.1.tar.gz​

[root]cd nginx-1.10.1

[root] ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --with-http_sub_module --with-http_gzip_static_module --with-http_stub_status_module --with-pcre=/usr/local/src/pcre-8.38 --with-zlib=/usr/local/src/zlib-1.2.8 --with-openssl=/usr/local/src/openssl-1.0.2h

[root]make && make install

提示:./configure --help可以查看编译选项

注意这里

http_image_filter_module是nginx提供的集成图片处理模块,需要gd-devel的支持,错误信息如下:
./configure: error: the HTTP image filter module requires the GD library.
You can either do not enable the module or install the libraries.
  • 1
  • 2

解决方法:

yum -y install gd-devel

5、配置Nginx

1)修改配置文件

[root]vi /usr/local/nginx/conf/nginx.conf

若需要,则将http -> server -> server_name改为服务器的外网ip地址,或你的网站域名​

2)配置开机自启动

方法一:在/etc/rc.d/rc.local文件最后增加一行脚本

[root]/usr/local/nginx/sbin/nginx

方法二:将Nginx加入服务,新增/etc/init.d/nginx脚本,内容请见nginx脚本,然后设置开机自启动:

[root]chmod +x /etc/init.d/nginx

[root]chkconfig nginx on #设置开启自启动后会自动将其加入服务

3)启动Nginx

若Nginx已加入服务,则用service命令启动服务

[root]service nginx start

否则运行Nginx程序

[root]/usr/local/nginx/sbin/nginx

二、安装MySQL

1、安装ncurses-devel

[root]yum install ncurses-devel -y

 

2、安装cmake

[root]wget https://cmake.org/files/v3.5/cmake-3.5.2.tar.gz

 

[root]tar -xvzf cmake-3.5.2.tar.gz

[root]cd cmake-3.5.2

[root] ./configure --prefix=/usr/local/cmake

[root]make && make install

[root]export PATH=$PATH:/usr/local/cmake/bin#临时加入PATH环境变量

3、安装mysql

同nginx一样,创建一个mysql账号专门用于运行mysql

[root]groupadd mysql

[root]useradd -g mysql mysql -s /sbin/false

[root]wget http://dev.mysql.com/Downloads/MySQL-5.6/mysql-5.6.31.tar.gz

[root]tar -xvzf mysql-5.6.31.tar.gz

[root]cd mysql-5.6.31

[root]

cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DSYSCONFDIR=/etc -DMYSQL_DATADIR=/usr/local/mysql/data -DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci

cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \#mysql安装到的路径

-DSYSCONFDIR=/etc \  #mysql配置文件(my.cnf)路径

-DMYSQL_DATADIR=/usr/local/mysql/data \ #data目录路径

-DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock \  #sock存放路径

-DDEFAULT_CHARSET=utf8 \  #默认字符集

-DDEFAULT_COLLATION=utf8_general_ci#默认字符集校验

[root]make && make install

4、配置mysql

以下所有操作都在/usr/local/mysql路径下执行。

1)初始化数据库

[root]scripts/mysql_install_db --user=mysql --datadir=/usr/local/mysql/data

2)拷贝配置文件

[root]cp support-files/my-default.cnf /etc/my.cnf

3)启动mysql

[root]cp support-files/mysql.server /etc/init.d/mysqld#服务名也可以取做mysql,随你

[root]chkconfig mysqld on

[root]service mysqld start

4)修改mysql的root账号密码

[root]bin/mysql_secure_installation

然后设置密码,并进行一些配置

5)登录mysql

[root]mysql -uroot -p

然后输入密码登录。若运气不好的话(比如我),输入密码登录,里面关闭,并输出segment fault提示,那么就需要修改源代码并重新编译安装了。打开/usr/local/src/php.5.6.31/cmd-line-utils/libedit/terminal.c,在terminal_set函数中:

a、注释char buf[TC_BUFSIZE];一行

b、将area = buf改成area = NULL

然后重新编译安装。

三、安装php

本文最小安装php。

1、安装php

[root]wget http://cn2.php.net/distributions/php-5.6.22.tar.bz2

[root]tar -xvjf php-5.6.22.tar.bz2

[root]cd php-5.6.22

[root] ./configure --prefix=/usr/local/php \ #php安装路径

--with-libdir=lib64 \ #64位操作系统需要

--enable-mysqlnd \ 

--with-mysqli=mysqlnd \ 

--with-pdo-mysql=mysqlnd \ 

--with-mysql_sock=/var/lib/mysql/mysql.sock \

--enable-fpm \ 

--enable-opcache \ 

--with-mhash  \ 

--with-ldap#本人的项目用到,需yum install openldap-devel

[root]make && make install

2、配置php

以下命令都是在/usr/local/php路径下执行。

1)查看php.ini文件存放路径

[root]bin/php --ini

Configuration File (php.ini) Path: /usr/local/php/lib

[root]cp /usr/local/src/php-5.6.22/php.ini-production lib/php.ini

2)配置php.ini

 

a.关闭在http头中显示php版本信息​

​     expose_php = Off

b. 设置时区

     date.timezone = PRC​

3、配置php-fpm

以下命令都是在/usr/local/php路径下执行。

[root]cp etc/php-fpm.conf.default etc/php-fpm.conf

1)去掉25行 ;pid = run/php-fpm.pid  前面的分号,使之生效

2)第148行改为 user = nginx 设置php-fpm运行账号为nginx

3)第149行改为 group = nginx #设置php-fpm运行组为nginx

4)可选。php-fpm默认采用tcp通信,若需要采用unix socket通讯,则配置如下

 

listen = /dev/shm/php-fpm.sock​

listen.owner = nginx​

listen.group = nginx

4、启动php-fpm

[root]cp /usr/local/src/php-5.6.22/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

[root]chmod +x /etc/init.d/php-fpm

[root]chkconfig php-fpm on

[root]service php-fpm start

5、配置nginx支持php

[root]vi /usr/local/nginx/conf/nginx.conf

1)顶部行改成 user nginx nginx;

2)将

 

location ​/ {

      root    html;

      index index.html index.htm

}​

改为:

​location ​/ {

      root    /www;

      index index.php index.html index.htm

}​​

注:需要将web根目录/www开放权限给nginx账号:chown nginx:nginx /www

3)取消location ~ \.php$ { 一段的注释,如下:

  location ~ \.php$ {
            root           /www;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

这是php-fpm采用tcp通信时的配置,若其采用unix socket通信,则fastcgi_pass一行需该为:

fastcgi_pass   unix:/dev/shm/php-fpm.sock;

6、配置php支持mysql

其实到这一步,php已经支持mysqli及pdo_mysql了(由于mysql_connect等函数已经废弃,所以在编译php时没有--with-mysql)。但是用mysqli_connect连接本机时,只能使用'127.0.0.1',而不能使用'localhost'来连接,原因是:mysql通过tcp连接到127.0.0.1,通过unix socket连接到localhost。只要在php.ini设置mysqli及pdo_mysql的default_socket为/var/lib/mysql/mysql.sock即可。貌似在编译php时带上--with-mysql-sock=/var/lib/mysql/mysql.sock选项就不用配置php.ini中的default_socket了。

7、重启php-fpm及nginx

[root]service php-fpm restart

[root]service nginx restart

8、测试nginx对php的支持

新建/www/info.php文件,内容如下:

<?php phpinfo(); ?>

在浏览器中查看:localhost/info.php

9、测试对mysqli及pdo_mysql的支持

新建/www/mysql.php文件,测试mysqli时内容为:

<?php var_dump(mysqli_connect('localhost', 'root', '111111')); ?>

测试pdo_mysql时内容为:

<?php var_dump(new PDO('mysql:host=localhost;db=mysql', 'root', '111111')); ?>

上面的root和111111为mysql账号和密码。

分别在浏览器中查看:localhost/mysql.php,正常时,显示内容分别含”object(mysqli)“和”object(PDO)“。

 

 

-------------------------------

安装完mysql-server 
会提示可以运行mysql_secure_installation。运行mysql_secure_installation会执行几个设置:

a)为root用户设置密码
b)删除匿名账号
c)取消root用户远程登录
d)删除test库和对test库的访问权限

e)刷新授权表使修改生效

通过这几项的设置能够提高mysql库的安全。建议生产环境中mysql安装这完成后一定要运行一次mysql_secure_installation,详细步骤请参看下面的命令:

复制代码
代码如下:

[root@server1 ~]#
mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS
RECOMMENDED FOR ALL MySQL
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP
CAREFULLY!
In order to log into MySQL to secure it, we'll need the
current
password for the root user. If you've just installed MySQL,
and
you haven't set the root password yet, the password will be blank,
so
you should just press enter here.
Enter current password for root (enter for
none):<–初次运行直接回车
OK, successfully used
password, moving on…
Setting the root password ensures that nobody can log
into the MySQL
root user without the proper authorisation.
Set root
password? [Y/n] <–
是否设置root用户密码,输入y并回车或直接回车
New password: <– 设置root用户的密码
Re-enter new password: <– 再输入一次你设置的密码
Password updated
successfully!
Reloading privilege tables..
… Success!
By default, a
MySQL installation has an anonymous user, allowing anyone
to log into MySQL
without having to have a user account created for
them. This is intended only
for testing, and to make the installation
go a bit smoother. You should
remove them before moving into a
production environment.
Remove anonymous
users? [Y/n] <–
是否删除匿名用户,生产环境建议删除,所以直接回车
… Success!
Normally, root should only be
allowed to connect from 'localhost'. This
ensures that someone cannot guess
at the root password from the network.
Disallow root login remotely?
[Y/n]
<–是否禁止root远程登录,根据自己的需求选择Y/n并回车,建议禁止
… Success!
By default, MySQL
comes with a database named 'test' that anyone can
access. This is also
intended only for testing, and should be removed
before moving into a
production environment.
Remove test database and access to it? [Y/n] <– 是否删除test数据库,直接回车
- Dropping test
database…
… Success!

转载于:https://www.cnblogs.com/iswill/p/8656494.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值