安装wget:yum install wget -yphp
安装gcc及g++:yum install gcc gcc-c++ -yhtml
后续全部源代码都下载到/usr/local/src目录mysql
防火墙更改配置及关闭selinux见另外一篇文章《LAMP环境搭建》linux
Nginx依赖pcre(重写rewrite)、zlib(网页gzip压缩)及openssl(加密传输)。nginx
[root]wget http://pilotfiber.dl.sourceforge.net/project/pcre/pcre/8.38/pcre-8.38.tar.gzc++
[root]tar -xvzf pcre-8.38.tar.gzweb
[root]cd pcre-8.38sql
[root] ./configure --prefix=/usr/local/pcre数据库
[root]make && make installapi
[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
[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
为了安全起见,建立一个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能够查看编译选项
注意这里:
./configure: error: the HTTP image filter module requires the GD library. You can either do not enable the module or install the libraries.
解决方法:
yum -y install gd-devel
[root]vi /usr/local/nginx/conf/nginx.conf
若须要,则将http -> server -> server_name改成服务器的外网ip地址,或你的网站域名
方法一:在/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 #设置开启自启动后会自动将其加入服务
若Nginx已加入服务,则用service命令启动服务
[root]service nginx start
不然运行Nginx程序
[root]/usr/local/nginx/sbin/nginx
[root]yum install ncurses-devel -y
[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环境变量
同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
如下全部操做都在/usr/local/mysql路径下执行。
[root]scripts/mysql_install_db --user=mysql --datadir=/usr/local/mysql/data
[root]cp support-files/my-default.cnf /etc/my.cnf
[root]cp support-files/mysql.server /etc/init.d/mysqld#服务名也能够取作mysql,随你
[root]chkconfig mysqld on
[root]service mysqld start
[root]bin/mysql_secure_installation
而后设置密码,并进行一些配置
[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。
[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
如下命令都是在/usr/local/php路径下执行。
[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
a.关闭在http头中显示php版本信息
expose_php = Off
b. 设置时区
date.timezone = PRC
如下命令都是在/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
[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
[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;
其实到这一步,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了。
[root]service php-fpm restart
[root]service nginx restart
新建/www/info.php文件,内容以下:
<?php phpinfo(); ?>
在浏览器中查看:localhost/info.php
新建/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 canaccess. This is also intended only for testing, and should be removedbefore moving into a production environment.Remove test database and access to it? [Y/n] <– 是否删除test数据库,直接回车- Dropping test database…… Success!