CentOS下搭建LAMP

CentOS下搭建LAMP

前言:
我这里会讲解lamp架构的安装到配置,以及安装过程中的一些报错点

提示:这里的安装包括源码安装和yum安装

什么是lamp?

httpd服务通常和mysql、php一起使用,而这些通常都在linux系统下去运行,也就是我们所说的lamp架构,所以lamp是个架构的名称。 lamp = linux + apache + mysql + php

文章目录

  • CentOS下搭建LAMP
    • @[TOC](文章目录)
  • 一、源码编译安装部署lamp
    • ★安装之前的准备工作★
    • 1.安装httpd
    • 2.安装mysql
    • 3.安装php
    • 测试阶段
  • 二、yum安装lamp
  • 错误集锦

一、源码编译安装部署lamp

★安装之前的准备工作★

我这里使用的环境如下:
系统:CentOS7.9
ip地址:192.168.31.101
安装包:
apr-1.7.0.tar.gz
apr-util-1.6.1.tar.gz
httpd-2.4.48.tar.gz
mysql-boost-5.7.36.tar.gz
php-7.4.28.tar.gz

注意:这些安装包的版本也很重要,需要注意版本的兼容性,在版本兼容上我也是踩了很多坑

安装包下载地址

apr和apr-util包下载地址:http://archive.apache.org/dist/apr/
httpd下载地址:https://httpd.apache.org/
mysql下载地址:https://downloads.mysql.com/archives/community/
php下载地址:https://www.php.net/downloads.php

关闭防火墙和selinux

systemctl stop firewalld
setenforce 0

安装epel源,这样方便安装一些依赖包,要不然yum会没有一些包,再安装所需要的编译工具

yum install -y epel-release
yum clean all && yum makecache
yum -y install gcc gcc-g++ cmake make

现在就可以正式开始安装了。。。。。。
这里的安装顺序建议是httpd、mysql、php,httpd和mysql的安装顺序都可以,php一般放到最后来,它是httpd和mysql的中间人,协调工作。

1.安装httpd

1.安装编译httpd时所需要的依赖包

yum -y install expat-devel pcre pcre-devel
#安装apr-1.7.0
tar zxf apr-1.7.0.tar.gz && cd apr-1.7.0
./configure --prefix=/usr/local/apr
make && make install
#安装apr-util-1.6.1
tar zxf apr-util-1.6.1.tar.gz && cd apr-util-1.6.1
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
make && make install

expat-devel:用于支持网站解析HTML、XML文件
pcre:一个Perl函数库,包括perl 兼容的正则表达式库
pcre-devel:perl的接口开发包
apr、apr-util是apache的函数库

2.创建apache程序运行使用的用户

groupadd apache && useradd -M -s /sbin/nologin apache -g apache

3.编译安装httpd

./configure --prefix=/usr/local/httpd --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-modules=all --enable-mods-shared=all
make && make install

这里可以用./configure --help查询编译可以使用的一些参数
–prefix=/usr/local/httpd :指定安装路径

4.编辑httpd配置文件
下面两项参数是httpd支持php重要项
AddType application/x-httpd-php.php
DirectoryIndex index.php index.html

vim /usr/local/httpd/conf/httpd.conf
-----------------------------------------------------------
ServerRoot "/usr/local/httpd"
Listen 80
LoadModule authn_file_module modules/mod_authn_file.so
LoadModule authn_core_module modules/mod_authn_core.so
LoadModule authz_host_module modules/mod_authz_host.so
LoadModule authz_groupfile_module modules/mod_authz_groupfile.so
LoadModule authz_user_module modules/mod_authz_user.so
LoadModule authz_core_module modules/mod_authz_core.so
LoadModule access_compat_module modules/mod_access_compat.so
LoadModule auth_basic_module modules/mod_auth_basic.so
LoadModule reqtimeout_module modules/mod_reqtimeout.so
LoadModule filter_module modules/mod_filter.so
LoadModule mime_module modules/mod_mime.so
LoadModule log_config_module modules/mod_log_config.so
LoadModule env_module modules/mod_env.so
LoadModule headers_module modules/mod_headers.so
LoadModule setenvif_module modules/mod_setenvif.so
LoadModule version_module modules/mod_version.so
LoadModule unixd_module modules/mod_unixd.so
LoadModule status_module modules/mod_status.so
LoadModule autoindex_module modules/mod_autoindex.so
LoadModule dir_module modules/mod_dir.so
LoadModule alias_module modules/mod_alias.so
LoadModule php7_module        modules/libphp7.so
<IfModule unixd_module>
User apache
Group apache
</IfModule>
ServerAdmin www.apache.com
ServerName www.apache.com
<Directory />
    AllowOverride none
    Require all denied
</Directory>
DocumentRoot "/usr/local/httpd/htdocs"
<Directory "/usr/local/httpd/htdocs">
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>
<IfModule dir_module>
    DirectoryIndex index.html
    DirectoryIndex index.php index.html
</IfModule>
<Files ".ht*">
    Require all denied
</Files>
ErrorLog "logs/error_log"
LogLevel warn
<IfModule log_config_module>
    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    LogFormat "%h %l %u %t \"%r\" %>s %b" common
    <IfModule logio_module>
      LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
    </IfModule>
    CustomLog "logs/access_log" common
</IfModule>
<IfModule alias_module>
    ScriptAlias /cgi-bin/ "/usr/local/httpd/cgi-bin/"
</IfModule>
<IfModule cgid_module>
</IfModule>
<Directory "/usr/local/httpd/cgi-bin">
    AllowOverride None
    Options None
    Require all granted
</Directory>
<IfModule headers_module>
    RequestHeader unset Proxy early
</IfModule>
<IfModule mime_module>
    TypesConfig conf/mime.types
    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz
    AddType application/x-httpd-php .php
</IfModule>
<IfModule proxy_html_module>
Include conf/extra/proxy-html.conf
</IfModule>
<IfModule ssl_module>
SSLRandomSeed startup builtin
SSLRandomSeed connect builtin
</IfModule>

可以用apachectl -t来检验配置文件是否正确,如下图所示
在这里插入图片描述
5.启动apache服务

启动apache服务
/usr/local/httpd/bin/apachectl start

查看服务启动状态(hhtpd默认是80端口)
netstat -anptu |grep 80 |grep httpd
httpd状态这里可以看到80端口已经启用,说明成功了

6.添加httpd服务到systemctl并优化服务命令路径(补充,也可以不要这一步骤,看个人)

#copy管理脚本到/etc/init.d下改名为httpd
cp /usr/local/httpd/bin/apachectl /etc/init.d/httpd
#添加启动级别参数
sed -i '2c \#chkconfig\: 35 85 15' /etc/init.d/httpd
#使用chkconfig添加httpd服务
chkconfig --add httpd
#用systemctl重启生效服务生效,查看状态
systemctl restart httpd
systemctl status httpd
#优化命令路径
ln -s /usr/local/httpd/bin/* /usr/bin

2.安装mysql

1.yum安装mysql所需要的依赖包

yum -y install openssl openssl-devel prce pcre-devel bison bison-devel ncurses-devel

2.创建mysql用户给mysql程序使用

groupadd mysql && useradd -M -s /sbin/nologin mysql -g mysql

3.解压安装包,使用cmake工具编译安装mysql

#解压安装包
tar zxf mysql-boost-5.7.36.tar.gz && cd mysql-5.7.36
#cmake编译安装mysql
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data -DSYSCONFDIR=/etc -DWITH_BOOST=boost -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock -DMYSQL_TCP_PORT=3306 -DENABLED_LOCAL_INFILE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci
make && make install

注意:在编译的时候注意一些参数跟下面第4点和第5点的参数和目录有联系,修改需谨慎,免得服务起不来找问题麻烦

编译的时间可能会等的有点久.。。。。。。就耐心等待吧!!!

4.使用mysql自带的mysqld命令初始化数据库,准备工作

/usr/local/mysql/bin/mysqld --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --pid-file=/usr/local/mysql/run/mysql.pid --initialize

这里初始化完成之后你会发现在你安装mysql的目录会自动生成data目录,这里还加载了mysql的pid文件的生成目录,记载mysql的pid进程号

5.创建准备mysql的一些工作目录

#创建日志目录并创建错误日志文件
mkdir -p /usr/local/mysql/run /usr/local/mysql/logs
touch /usr/local/mysql/logs/mysql_error.log
#把mysql的安装目录属主改为mysql,授权mysql程序pid运行目录755
chown -R mysql.mysql /usr/local/mysql
chmod 755 /usr/local/mysql/run

6.编辑mysql的配置文件

vim /etc/my.cnf
-----------------------------------------
[client]  
socket = /usr/local/mysql/mysql.sock
[mysqld]
skip-grant-tables
port = 3306 
user = mysql
character-set-server=utf8
socket = /usr/local/mysql/mysql.sock
pid-file = /usr/local/mysql/run/mysql.pid
log-error = /usr/local/mysql/logs/mysql_error.log
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
[mysqld_safe]
pid-file = /usr/local/mysql/run/mysql.pid
log-error = /usr/local/mysql/logs/mysql_error.log

7.启动服务并初始化服务密码
启动脚本在/usr/local/mysql/support-files这个目录下

/usr/local/mysql/support-files/mysql.server start

查看服务启动状态(mysql默认是3306端口)
netstat -anptu |grep 3306
在这里插入图片描述登录数据库设置root密码

mysql -uroot -p

因为配置文件设置了skip-grant-tables参数,所以不需要密码直接进入,开始设置密码

flush privileges;
#进入mysql库下,修改root用户密码为123456
use mysql;
set password for root@localhost=password('123456');
#默认是只能本地登录,授权root用户所有地址都能登录
grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;
flush privileges;

修改配置文件把skip-grant-tables这个参数注释掉(前面加上#号就行)或者删除,再重启服务
#skip-grant-tables
这里大家可以先去完成第8步直接使用systemctl命令重启

pkill mysqld
/usr/local/mysql/support-files/mysql.server start

这里我们就会发现不要密码的话就无法登录了,提示密码错误如下图
在这里插入图片描述mysql安装就完成了,可以使用了!!!

8.添加mysqld服务到systemctl并优化服务命令路径(补充,也可以不要这一步骤,看个人)

#mysqld服务加入systemctl方便管理,跟上面httpd服务原理一样
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
chkconfig --add mysqld
systemctl restart mysqld
systemctl status mysqld
#优化命令路径
ln -s /usr/local/mysql/bin/* /usr/bin

3.安装php

1.yum安装php所需要的依赖包

yum -y install php-bcmath php-mbstring sqlite-devel oniguruma-devel curl-devel freetype-devel libjpeg-devel libpng-devel libxslt-devel libxml2-devel zlib-devel

2.解压php安装包并编译

tar zxf php-7.4.28.tar.gz && cd php-7.4.28
 ./configure --prefix=/usr/local/php7.4 --with-apxs2=/usr/local/httpd/bin/apxs --enable-gd --with-jpeg --with-freetype --with-curl --with-zlib --with-zlib-dir --with-mysqli --with-config-file-path=/usr/local/php7.4 --enable-mysqlnd --enable-mbstring --enable-fpm --enable-bcmath --enable-sockets --enable-shared --with-gettext --with-openssl --with-pdo-mysql
 make && make install

这里也同样可以使用./configure --help查询编译可以使用的一些参数
3.配置php配置文件

vim /usr/local/php7.4/php.ini
------------------------------------------
[PHP]
date.timezone = "Asia/Shanghai"
engine = On
short_open_tag = Off
precision = 14
output_buffering = 4096
zlib.output_compression = Off
implicit_flush = Off
unserialize_callback_func =
serialize_precision = -1
disable_functions =
disable_classes =
zend.enable_gc = On
zend.exception_ignore_args = Off
expose_php = On
max_execution_time = 300
max_input_time = 300
memory_limit = 128M
post_max_size = 16M
error_reporting = E_ALL
display_errors = On
display_startup_errors = On
log_errors = On
log_errors_max_len = 1024
ignore_repeated_errors = Off
ignore_repeated_source = Off
report_memleaks = On
variables_order = "GPCS"
request_order = "GP"
register_argc_argv = Off
auto_globals_jit = On
auto_prepend_file =
auto_append_file =
default_mimetype = "text/html"
default_charset = "UTF-8"
doc_root =
user_dir =
enable_dl = Off
file_uploads = On
upload_max_filesize = 2M
max_file_uploads = 20
allow_url_fopen = On
allow_url_include = Off
default_socket_timeout = 60
extension=curl
extension=fileinfo
extension=gd2
extension=mysqli
extension=pdo_mysql
extension=pdo_odbc
[CLI Server]
cli_server.color = On
[Date]
[filter]
[iconv]
[imap]
[intl]
[sqlite3]
[Pcre]
[Pdo]
[Pdo_mysql]
pdo_mysql.default_socket=/usr/local/mysql/mysql.sock
[Phar]
[mail function]
SMTP = localhost
smtp_port = 25
mail.add_x_header = Off
[ODBC]
odbc.allow_persistent = On
odbc.check_persistent = On
odbc.max_persistent = -1
odbc.max_links = -1
odbc.defaultlrl = 4096
odbc.defaultbinmode = 1
[MySQLi]
mysqli.max_persistent = -1
mysqli.allow_persistent = On
mysqli.max_links = -1
mysqli.default_port = 3306
mysqli.default_socket =/usr/local/mysql/mysql.sock
mysqli.default_host =
mysqli.default_user =
mysqli.default_pw =
mysqli.reconnect = Off
[mysqlnd]
mysqlnd.collect_statistics = On
mysqlnd.collect_memory_statistics = On
[OCI8]
[PostgreSQL]
pgsql.allow_persistent = On
pgsql.auto_reset_persistent = Off
pgsql.max_persistent = -1
pgsql.max_links = -1
pgsql.ignore_notice = 0
pgsql.log_notice = 0
[bcmath]
bcmath.scale = 0
[browscap]
[Session]
session.save_handler = files
session.use_strict_mode = 0
session.use_cookies = 1
session.use_only_cookies = 1
session.name = PHPSESSID
session.auto_start = 0
session.cookie_lifetime = 0
session.cookie_path = /
session.cookie_domain =
session.cookie_httponly =
session.cookie_samesite =
session.serialize_handler = php
session.gc_probability = 1
session.gc_divisor = 1000
session.gc_maxlifetime = 1440
session.referer_check =
session.cache_limiter = nocache
session.cache_expire = 180
session.use_trans_sid = 0
session.sid_length = 26
session.trans_sid_tags = "a=href,area=href,frame=src,form="
session.sid_bits_per_character = 5
[Assertion]
zend.assertions = 1
[COM]
[mbstring]
[gd]
[exif]
[Tidy]
tidy.clean_output = Off
[soap]
soap.wsdl_cache_enabled=1
soap.wsdl_cache_dir="/tmp"
soap.wsdl_cache_ttl=86400
soap.wsdl_cache_limit = 5
[sysvshm]
[ldap]
ldap.max_links = -1
[dba]
[opcache]
[curl]
[openssl]
[ffi]

4.重启httpd服务来重新加载php配置

systemctl restart httpd

优化php命令路径

ln -s /usr/local/php7.4/bin/* /usr/bin
ln -s /usr/local/php7.4/sbin/* /usr/sbin

测试阶段

编辑php网页来测试lamp架构环境的连通性和可行性
httpd的网页默认路径是安装目录下的htdocs目录下我的目录也就是/usr/local/httpd/htdocs

vim /usr/local/httpd/htdocs/index.php
-------------------------------------------
<?php
$servername = "localhost";
$username = "root";
$password = "123456";
try {
    $conn = new PDO("mysql:host=$servername;port=3306", $username, $password);
echo "mysql数据库连接成功!!!
"
; } catch(PDOException $e) { echo $e->getMessage(); } ?> <?php echo "mysqli方式连接mysql数据库:"; $a=mysqli_connect("localhost","root","123456"); if($a){echo "数据库连接成功!!!";}else{echo "数据库连接失败!!!";} ?> <?php phpinfo(); ?>

注意:记得删除httpd原来的index.html文件,要不会优先访问httpd原来的html文件!!!

浏览器访问192.168.31.101,出现页面如下图:
CentOS下搭建LAMP_第1张图片这样说明了lamp架构搭建成功!!!

二、yum安装lamp

#安装nginx,mariadb,php
yum -y install httpd php php-fpm php-mysql mariadb mariadb-server
#安装其他模块包
yum -y install httpd-manual mod_ssl mod_perl mod_auth_mysql
yum -y install php-gd php-xml php-mbstring php-ldap php-pear php-xmlrpc php-devel
yum -y install mysql-connector-odbc mysql-devel libdbi-dbd-mysql
systemctl start httpd mariadb
#初始化数据库
mysql_secure_installation

记得在httpd.conf配置加入下面两个配置项:
AddType application/x-httpd-php.php
DirectoryIndex index.php index.html


错误集锦

提示:这里对安装过程中出现的一些错误进行记录以及解决方法
错误1:编译apr-util-1.6.1出现错误
xml/apr_xml.c:35:19: fatal error: expat.h: No such file or directory

yum -y install expat-devel

错误2:编译mysql-5.7.36出现错误
checking for termcap functions library… configure: error: No curses/termcap library found

yum install ncurses

错误3:Configure: error: xml2-config not found. Please check your libxml2 installation.

yum install libxml2 libxml2-devel 

错误4:Configure: error: libjpeg.(also) not found.

yum install libjpeg libjpeg-devel

错误5:Configure: error: Please reinstall the BZip2 distribution

yum install bzip2 bzip2-devel

错误6:没有安装snmp开发包
configure: error: snmp.h not found. Check your SNMP installation.

yum install net-snmp net-snmp-devel

你可能感兴趣的:(Web服务与群集,apache,php,centos,系统架构,mysql)