对于刚拿到手的服务器,一些环境都是需要重新安装,其实大多是按照如下方式,本文按照php环境lnmp为例举例说明常见的一些环境搭建方法,安装顺序nginx->mysql->php 且统一
安装在/usr/local目录下
安装编译依赖
yum -y install gcc pcre-devel zlib-devel openssl openssl-devel
下载自己喜欢的nginx版本
wget https://nginx.org/download/nginx-1.19.1.tar.gz
解压并更改名称
tar -zxvf nginx-1.19.1.tar.gz
mv nginx-1.19.1.tar.gz nginx
编译和安装
./configure --prefix=/usr/local/nginx
make & make install
./sbin/nginx -t (检测是否安装成功)
./sbin/nginx 启动服务
这里以mysql 5.6的安装为例,通过编译安装的方式
下载源码包
wget https://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz
卸载系统自带的mariadb
rpm -qa|grep mariadb 列出已安装的mariadb文件名
rpm -e --nodeps 文件名
创建mysql用户组
groupadd mysql
useradd -g mysql mysql
解压安装包并重名了
tar -zxvf mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz
mv mysql-5.6.36 mysql
进行安装
- cd /usr/local/mysql
- chown -R mysql:mysql ./ 修改当前目录拥有着为mysql用户
- yum -y install autoconf 安装Data:Dumper模块
- ./scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/ 安装数据库
- chown -R mysql:mysql data 修改当前data目录的拥有者为mysql用户
设置开机启动
cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld
增加mysqld服务控制脚本执行权限
chmod +x /etc/rc.d/init.d/mysqld
将mysqld服务加入到系统服务
chkconfig --add mysqld
检查mysqld服务是否已经生效
chkconfig --list mysqld
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
表明mysqld服务已经生效,在2、3、4、5运行级别随系统启动而自动启动,以后可以使用service命令控制mysql的启动和停止
启动mysql服务
service mysqld start
将mysql的bin目录加入PATH环境变量
vim ~/.bash_profile
export PATH=$PATH:/usr/local/mysql/bin
source ~/.bash_profile 配置文件生效
修改mysql root账户密码为123456
mysql -uroot -p (密码首次是系统的登陆密码)
use mysql;
update user set password=password('123456') where user='root' and host='localhost';
flush privileges;
设置远程主机登录
GRANT ALL PRIVILEGES ON . TO ‘root’@’%’ IDENTIFIED BY ‘123456’ WITH
GRANT OPTION;
默认源码安装的mysql的配置文件my.cnf 在源码目录下
如/usr/local/nginx/my.cnf
1.启动:/etc/init.d/mysqld start
2.停止:/etc/init.d/mysqld stop
3.重启:/etc/init.d/mysqld restart
mysql的配置Field * doesn’t have a default value
修改 my.cnf 修改 sql-mode=“NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION”
本文我们通过代码编译安装php,建议新手或者需要对服务器较强把控力度的使用,如果简单使用直接通过yum安装方式省心 这里yum安装就不做介绍了
下载php安装包
wget http://cn2.php.net/distributions/php-5.6.37.tar.gz
tar zxf php-5.6.37.tar.gz
mv php-5.6.37 php
cd php
安装相关依赖
yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers gd gd2 gd-devel gd2-devel perl-CPAN pcre-devel
配置php编译项
./configure
–prefix=/usr/local/php
–with-config-file-path=/usr/local/php/etc
–enable-inline-optimization
–disable-debug
–disable-rpath
–enable-shared
–enable-opcache
–enable-fpm
–with-mysql=mysqlnd
–with-mysqli=mysqlnd
–with-pdo-mysql=mysqlnd
–with-gd
–with-jpeg-dir
–with-png-dir
–with-freetype-dir
–with-gettext
–enable-mbstring
–with-zlib-dir
–with-zlib
–enable-zip
–with-bz2
–enable-bcmath
–with-libxml-dir
–enable-soap
–enable-gd-native-ttf
–enable-ftp
–enable-exif
–enable-pcntl
–enable-shmop
–enable-sysvmsg
–enable-sysvsem
–enable-sysvshm
–with-pear
–with-readline
–enable-sockets
–with-curl
–with-mcrypt
–with-mhash
–with-openssl
yum -y install readline-devel
https://www.openssl.org/source/openssl-1.0.2k.tar.gz
解压后进入文件夹
./config
make
make install
mv /usr/local/bin/openssl /usr/local/bin/openssl1.1.1
ln -s /usr/local/ssl/bin/openssl /usr/local/bin/openssl
问题2 处理以后 重新配置configure配置项,添加–with-openssl=/usr/local/ssl
./configure \
--prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--enable-inline-optimization \
--disable-debug \
--disable-rpath \
--enable-shared \
--enable-opcache \
--enable-fpm \
--with-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-gettext \
--enable-mbstring \
--with-zlib-dir \
--with-zlib \
--enable-zip \
--with-bz2 \
--enable-bcmath \
--with-libxml-dir \
--enable-soap \
--enable-gd-native-ttf \
--enable-ftp \
--enable-exif \
--enable-pcntl \
--enable-shmop \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--with-pear \
--with-readline \
--enable-sockets \
--with-curl \
--with-mcrypt \
--with-mhash \
--with-openssl=/usr/local/ssl
开始安装php
make && make install
提示:如果make过程中有错误,需要重新执行configure的话 先尝试make clean 后执行configure ,如果还是有make的过程错误 则删除源码包,重新解压重头执行流程
配置php
cp /usr/local/php/php.ini-production /usr/local/php/etc/php.ini
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
ln -s /usr/local/php/sbin/php-fpm /usr/bin/php-fpm(php-fpm启动命令添加软连接)
ln -s /usr/local/php/bin/php /usr/bin/php
ln -s /usr/local/php/bin/phpize /usr/bin/phpize(插件安装工具 添加软连接)
启动php-fpm
php-fpm
至此php的安装环境就结束了。
安装过程中多多少少可能会缺一些插件,对于插件的安装可以从参考我上一篇文章php插件安装
这里也顺道做一下总结 插件的编译安装大概也就这几步:
执行phpize,生成初始化编译环境
/usr/local/php/bin/phpize
如果没有安装autoconf,则安装用来生成自动配置源码编译的工具
yum install autoconf -y
执行配置(这里以pdf-mysql为例,这里需要关联/usr/local/mysql/)
./configure --with-php-config=/usr/local/php/bin/php-config --with-pdo-mysql=/usr/local/mysql/
对于不需要关联的插件直接执行
./configure --with-php-config=/usr/local/php/bin/php-config
执行编译
make && make install
编译完成后会提示编译结果的输出目录,可进入查看如果有对应的so文件可以执行添加so文件到php.ini 目录中
echo ‘extension=pdo_mysql.so’ >> /usr/local/php/lib/php.ini
php-fpm执行完成以后会默认在9000端口运行,检查当前php-fpm绑定的进程可以参考
ps aux|grep php-fpm
location ~ \.php$ {
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_buffers 8 16k;
fastcgi_buffer_size 32k;
}