linux LAMP搭建

[root@lamp ~]# tar xf apr-1.6.5.tar.gz -C /usr/src/
[root@lamp ~]# tar xf apr-util-1.6.1.tar.gz -C /usr/src/
[root@lamp ~]# tar xf httpd-2.4.38.tar.gz -C /usr/src/
[root@lamp ~]# cd /usr/src/
[root@lamp src]# ls
apr-1.6.5  apr-util-1.6.1  debug  httpd-2.4.38  kernels
[root@lamp src]# mv  apr-1.6.5/   httpd-2.4.38/srclib/apr
[root@lamp src]# mv  apr-util-1.6.1/   httpd-2.4.38/srclib/apr-util

(3)编译安装Apache

[root@lamp src]# cd httpd-2.4.38/
[root@lamp httpd-2.4.38]# ./configure --prefix=/usr/local/httpd --enable-charset-lite --enable-rewrite --enable-cgi --enable-so && make && make install

模块简介:
mod_charset_lite 指定字符集转换或重新编码
mod_rewrite 提供一个基于规则的重写引擎来动态重写请求的url
mod_cgi 执行CGI脚本
mod_so 在启动或重新启动时将可执行代码和模块加载到服务器

参考:http://httpd.apache.org/docs/2.4/mod/#C

(4)安装后优化
修改程序用户程序组

[root@lamp ~]# useradd -M -s /sbin/nologin apache
[root@lamp ~]# chown -R apache.apache /usr/local/httpd/
[root@lamp ~]# sed -i 's/User daemon/User apache/' /usr/local/httpd/conf/httpd.conf
[root@lamp ~]# sed -i 's/Group daemon/Group apache/' /usr/local/httpd/conf/httpd.conf

修改servername

[root@lamp ~]# sed -i '/#ServerName/ s/#//' /usr/local/httpd/conf/httpd.conf

增加系统命令变量路径

[root@lamp ~]# echo 'PATH=$PATH:/usr/local/httpd/bin/' >> /etc/profile
[root@lamp ~]# . /etc/profile

创建启动脚本并设置开机自启

[root@lamp ~]# cp /usr/local/httpd/bin/apachectl /etc/init.d/httpd
[root@lamp ~]# sed -i '1a# chkconfig: 35 85 15' /etc/init.d/httpd 
[root@lamp ~]# head -2 /etc/init.d/httpd 
#!/bin/sh
# chkconfig: 35 85 15
[root@lamp ~]# chkconfig --add httpd

[root@lamp ~]# chmod +x /etc/rc.d/rc.local 
[root@lamp ~]# echo "/etc/init.d/httpd start" >> /etc/rc.d/rc.local
启动服务测试
[root@lamp ~]# /etc/init.d/httpd start
[root@lamp ~]# netstat -anpt | grep :80
tcp6       0      0 :::80                   :::*                    LISTEN      64521/httpd  

[root@lamp ~]# curl 192.168.1.66
<html><body><h1>It works!</h1></body></html>

2、安装MySQL
(1)安装依赖包

[root@lamp ~]# yum -y install cmake bison ncurses ncurses-devel autoconf openssl-devel

(2)编译安装MySQL

[root@lamp ~]# useradd -M -s /sbin/nologin mysql
[root@lamp ~]# tar xf mysql-5.6.43.tar.gz -C /usr/src/
[root@lamp ~]# cd /usr/src/mysql-5.6.43/
[root@lamp mysql-5.6.43]# cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DENABLED_LOCAL_INFILE=1 \
-DMYSQL_TCP_PORT=3306 \
-DSYSCONFDIR=/etc \
-DEXTRA_CHARSETS=all \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_SSL=system \
-DINSTALL_SHAREDIR=share \

选项参考:https://dev.mysql.com/doc/refman/5.6/en/source-configuration-options.html

[root@lamp mysql-5.6.43]# make && make install

(3)安装后优化

[root@lamp mysql-5.6.43]# rm -f /etc/my.cnf
[root@lamp mysql-5.6.43]# pwd
/usr/src/mysql-5.6.43
[root@lamp mysql-5.6.43]# cp support-files/my-default.cnf /etc/my.cnf
[root@lamp mysql-5.6.43]# chown -R mysql.mysql /usr/local/mysql/

[root@lamp ~]# echo 'PATH=$PATH:/usr/local/mysql/bin/' >> /etc/profile
[root@lamp ~]# . /etc/profile

(4)初始化数据库

[root@lamp ~]# /usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data

(5)添加为系统服务并设置为开机自启

[root@lamp ~]# cp /usr/src/mysql-5.6.43/support-files/mysql.server /etc/init.d/mysqld
[root@lamp ~]# chmod +x /etc/init.d/mysqld
[root@lamp ~]# chkconfig --add mysqld

(6)简单的安全加固

[root@lamp ~]# /etc/init.d/mysqld start
Starting MySQL.Logging to '/usr/local/mysql/data/lamp.err'.
 SUCCESS!
[root@lamp ~]# netstat -anpt | grep :3306
tcp6       0      0 :::3306                 :::*                    LISTEN      84304/mysqld  

[root@lamp ~]# mysql_secure_installation

设置密码
然后全选y

或仅修改mysql root密码:

[root@lamp ~]# mysqladmin -uroot password "123123"

登录测试

[root@lamp ~]# mysql -uroot -p
Enter password:

或:

[root@lamp ~]# mysql -uroot -p -h localhost -P 3306

3、安装PHP
(1)安装依赖包

[root@lamp ~]# yum -y install libjpeg-devel libpng-devel freetype-devel libxml2-devel zlib-devel curl-devel libicu-devel openssl-devel 

(2)源码安装PHP

[root@lamp ~]# tar xf php-7.3.2.tar.gz -C /usr/src/
[root@lamp ~]# cd /usr/src/php-7.3.2/
[root@lamp php-7.3.2]# ./configure  --prefix=/usr/local/php  --with-apxs2=/usr/local/httpd/bin/apxs  --with-mysql-sock=/tmp/mysql.sock  --with-pdo-mysql  --with-mysqli  --with-zlib  --with-curl  --with-gd  --with-jpeg-dir  --with-png-dir  --with-freetype-dir  --with-openssl  --enable-mbstring  --enable-xml  --enable-session  --enable-ftp  --enable-pdo  --enable-tokenizer  --enable-intl && make && make install

创建php.ini配置文件并配置数据库接口

[root@lamp php-7.3.2]# cp php.ini-development /usr/local/php/lib/php.ini
[root@lamp php-7.3.2]# vim /usr/local/php/lib/php.ini
1175 mysqli.default_socket = /tmp/mysql.sock

4、配置Apache解析PHP

[root@lamp ~]# vim /usr/local/httpd/conf/httpd.conf

……

258 <IfModule dir_module>
259     DirectoryIndex index.html index.php
260 </IfModule>
……
395     AddType application/x-compress .Z
396     AddType application/x-gzip .gz .tgz
397     AddType application/x-httpd-php .php
398     AddType application/x-httpd-php-source .phps
……
[root@lamp ~]# httpd -t
Syntax OK
[root@lamp ~]# /etc/init.d/httpd stop
[root@lamp ~]# /etc/init.d/httpd start

5、编写测试页测试

[root@lamp ~]# vim /usr/local/httpd/htdocs/1.php
<?php
phpinfo();
?>

猜你喜欢

转载自blog.csdn.net/qq_39109226/article/details/111245576
今日推荐