一、yum源直接搭建
1.安装epel源
CentOS自带的网络源有些软件包没有,epel源包含更多的软件包
yum install -y epel-release
2.安装mariadb
yum install -y mariadb mariadb-server
初始化mariadb
mysql_secure_installation
启动mariadb,并设为开机自启
3.安装php模块
yum install -y php php-mysql php-gd libjpeg\* php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-bcmath php-mhash php-fpm php-pecl-zendopcache
配置php-fpm
vim /etc/php-fpm.d/www.conf
listen = /var/run/php-fpm/fastcgi.socket
listen.owner = nginx
listen.group = nginx
user = nginx
group = nginx
php_flag[display_errors] = on
创建nginx用户,让nginx来管理php-fpm
groupadd nginx && useradd -r -g nginx -s /sbin/nologin nginx
修改权限
chown -R nginx.nginx /var/run/php-fpm
启动php-fpm,并设为开机自启
systemctl start php-fpm && systemctl enable php-fpm
3.安装nginx
yum install -y nginx
配置nginx
vim /etc/nginx/nginx.conf
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name localhost;
root /usr/share/nginx/html;
index index.php index.html;
...
location ~ \.php$ {
fastcgi_pass unix:/var/run/php-fpm/fastcgi.socket;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
启动nginx,并设为开机自启
systemctl start nginx && systemctl enable nginx
4.测试环境是否搭建成功
删除nginx家目录的主页文件,并添加php测试文件
rm -rf /usr/share/nginx/html/index.html && echo "<?php phpinfo(); ?>" > /usr/share/nginx/html/index.php
打开浏览器,输入你的ip地址,出现php版本界面,环境搭建完成
二、源码版LNMP环境的搭建
1.安装mysql
mysql官网:http://www.mysql.org
下载mysql5.7二进制包
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.26-el7-x86_64.tar.gz
安装mysql所需要的库
yum install -y perl libao-devel
解压并改名,添加环境变量
tar -xf mysql-5.7.26-el7-x86_64.tar.gz -C /usr/local && \
mv /usr/local/mysql-5.7.26-el7-x86_64/ /usr/local/mysql && \
echo "export PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile && \
source /etc/profile
创建mysql配置文件
创建配置文件目录
mkdir /usr/local/mysql/etc
创建配置文件
vim /usr/local/mysql/etc/my.cnf
[mysqld]
user=mysql
port=3306
datadir=/var/lib/mysql
pid-file=/var/run/mysql/mysql.pid
socket=/var/lib/mysql/mysql.sock
[mysqld_safe]
log-error=/var/log/mysql/mysql_err.log
[client]
socket=/var/lib/mysql/mysql.sock
创建mysql用户,配置文件所用到的目录,并初始化
groupadd mysql && useradd -r -g mysql -s /sbin/nologin mysql
创建配置文件所用到的目录
mkdir -p /var/log/mysql /var/lib/mysql /var/run/mysql
改变所属用户
chown -R mysql.mysql /var/log/mysql /var/lib/mysql /var/run/mysql
初始化
mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/var/lib/mysql
...
...root@localhost: _R#f2oxqp1oa
初始化后会生成随机密码,注意保存
配置启动脚本
复制启动脚本
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
修改启动脚本
vim /etc/init.d/mysql
basedir=/usr/local/mysql
datadir=/var/lib/mysql
conf=$basedir/etc/my.cnf
启动mysql
/etc/init.d/mysql start
登录mysql
mysql -uroot -p_R#f2oxqp1oa
修改mysql的密码
- 在已知密码的情况下修改
mysqladmin -uroot -p password "root"
- 在忘记密码的情况下,修改配置文件,在[mysqld]下加上以下参数,跳过授权表,登录时不需要验证密码
vim /usr/local/mysql/etc/my.cnf
[mysqld]
skip-grant-tables
重启mysql
/etc/init.d/mysql restart
然后就可以不要密码直接登录,登录后修改密码
mysql -uroot
mysql> update mysql.user set authentication_string=password("root") where user="root" and host="localhost";
修改成功后记得把[mysqld]文件的内容还原并重新启动
2.安装php
php官网:https://www.php.net/
下载php5.6源码包
wget https://www.php.net/distributions/php-5.6.40.tar.gz
下载php7.3源码包
wget https://www.php.net/distributions/php-7.3.6.tar.gz
2个版本的一些配置参数有改变,配置时一些选项在php5.6以上的版本被删除
安装编译工具以及所需要的依赖
首先安装epel源
yum install -y epel-release
安装编译工具
yum install -y gcc gcc-c++ openssl openssl-devel libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel
编译前配置
php5.6版本的配置
./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=/var/lib/mysql/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 --disable-ipv6 --with-pear --with-curl --with-openssl
php7.3版本的配置
./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 --enable-mysqlnd --with-mysqli --with-pdo-mysql=mysqlnd --with-mysql-sock=/var/lib/mysql/mysql.sock --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --enable-soap --enable-ftp --enable-mbstring --enable-exif --disable-ipv6 --with-pear --with-curl --with-openssl
二者的区别:
php7不再使用mysql的库来支持mysql的连接,而是启用了mysqlnd来支持,所以php7的编译已经不再使用–with-mysql参数指定mysql的安装位置了,若想支持mysql,需要设置–enable-mysqlnd,–with-mysqli和–with-pdo-mysql=mysqlnd参数
参数解析:
–prefix=/usr/local/php --指定php安装路径
–with-config-file-path=/usr/local/php/etc --指定php配置文件存放路径
–enable-fpm --是否开启php-fpm,必须加入该参数
–with-fpm-user=php-fpm --执行的用户
–with-fpm-group=php-fpm --执行的组
–with-mysql=/usr/local/mysql --mysql的安装路径
–with-mysql-sock=/var/lib/mysql/mysql.sock --mysql的socket文件路径
等待完成
可以通过在命令行界面输入echo $?
,如果返回值为0,代表配置成功
编译安装
make && make install
通过输入echo $?
,来判断编译安装是否成功
配置php
复制php.ini文件
cp php.ini-production /usr/local/php/etc/php.ini
新增php-fpm.conf配置文件
vim /usr/local/php/etc/php-fpm.conf
[global]
pid = /var/run/php/php-fpm.pid
error_log = /var/log/php/php-fpm_err.log
[www]
listen = /tmp/php-fpm.sock
listen.mode = 666
user = nginx
group = nginx
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
创建配置文件中指定的目录:
mkdir /var/log/php /var/run/php -p
创建php执行用户:
groupadd nginx && useradd -r -s /usr/sbin/nologin -g nginx nginx
修改目录权限:
chown -R nginx.nginx /var/log/php /var/run/php
拷贝php-fpm启动文件并修改:
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
vim /etc/init.d/php-fpm
php_fpm_PID=/var/run/php/php-fpm.pid --改为你指定的pid文件路径
赋予启动文件执行权限
chmod 755 /etc/init.d/php-fpm
测试php-fpm.conf语法是否正确
/usr/local/php/sbin/php-fpm -t
... /usr/local/php/etc/php-fpm.conf test is successful
启动php
/etc/init.d/php-fpm start
Starting php-fpm done
检查php-fpm进程
ps -ef | grep php-fpm
php安装完成
3.安装nginx
nginx官网:http://nginx.org/
下载nginx源码包
这里采用nginx1.16稳定版源码包
wget http://nginx.org/download/nginx-1.16.0.tar.gz
解压
tar xf nginx-1.16.0.tar.gz -C /usr/src/
编译前配置
cd /usr/src/nginx-1.16.0/
# 添加支持ssl证书模块
./configure --with-http_ssl_module
# 查看配置是否成功
echo $?
0
编译安装
make && make install
echo $?
0
添加环境变量
echo "export PATH=$PATH:/usr/local/nginx/sbin" >> /etc/profile && source /etc/profile
修改nginx配置文件
vim /usr/local/nginx/conf/nginx.conf
http {
...
server {
listen 80;
server_name localhost;
index index.html index.htm index.php;
root /usr/local/nginx/html;
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/tmp/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
}
}
}
测试nginx配置文件是否正确
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
nginx
4.测试php环境是否能够正常解析
删除nginx家目录的主页文件,并添加php测试文件
rm -rf /usr/share/nginx/html/index.html && echo "<?php phpinfo(); ?>" > /usr/share/nginx/html/index.php
打开浏览器,输入你的ip地址,出现php版本界面,环境搭建完成