部署LAMP

部署LAMP

安装httpd

// 安装vimwget
[root@localhost ~]# yum -y install vim wget

//安装开发工具包
[root@localhost ~]# yum groups mark install 'Development Tools' -y

//创建apache服务的用户和组
[root@localhost ~]# useradd -r -M -s /sbin/nologin apache
[root@localhost ~]# id apache
uid=994(apache) gid=991(apache)=991(apache)

// 安装依赖包
[root@localhost ~]# yum -y install openssl-devel pcre-devel expat-devel libtool gcc gcc-c++ make

// 下载和安装httpdapr以及apr-util
[root@localhost src]# wget  https://mirrors.tuna.tsinghua.edu.cn/apache/httpd/httpd-2.4.49.tar.gz
[root@localhost src]# wget  https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-1.7.0.tar.gz
[root@localhost src]# wget  https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-util-1.6.1.tar.gz

// 解压软件包
[root@localhost src]# tar xf apr-1.7.0.tar.gz 
[root@localhost src]# tar xf apr-util-1.6.1.tar.gz 
[root@localhost src]# tar xf httpd-2.4.49.tar.gz 

// 修改配置文件
[root@localhost src]# cd apr-1.7.0
[root@localhost apr-1.7.0]# vim configure
cfgfile="${ofile}T"
    trap "$RM \"$cfgfile\"; exit 1" 1 2 15
    # $RM "$cfgfile"        //将此行加上注释,或者删除此行

// 编译安装apr
[root@localhost apr-1.7.0]# ./configure --prefix=/usr/local/apr
[root@localhost apr-1.7.0]# make
[root@localhost apr-1.7.0]# make install

// 编译安装apr-util
[root@localhost apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/
[root@localhost apr-util-1.6.1]# make && make install

// 编译安装httpd
[root@localhost httpd-2.4.49]# ./configure --prefix=/usr/local/apache \
--enable-so \
--enable-ssl \
--enable-cgi \
--enable-rewrite \
--with-zlib \
--with-pcre \
--with-apr=/usr/local/apr \
--with-apr-util=/usr/local/apr-util/ \
--enable-modules=most \
--enable-mpms-shared=all \
--with-mpm=prefork
[root@localhost httpd-2.4.49]# make && make install

// 环境变量
[root@localhost ~]# which httpd
/usr/local/apache/bin/httpd
[root@localhost ~]# echo 'export PATH=/usr/local/apache/bin:$PATH' > /etc/profile.d/httpd.sh
[root@localhost ~]# source /etc/profile.d/httpd.sh
[root@localhost ~]# ln -s /usr/local/apache/include /usr/include/apache
[root@localhost ~]# ll /usr/include/
总用量 1760
-rw-r--r--.  1 root root   7456 3月  12 2021 aio.h
-rw-r--r--.  1 root root   2031 3月  12 2021 aliases.h
-rw-r--r--.  1 root root   1203 3月  12 2021 alloca.h
-rw-r--r--.  1 root root   4350 3月  12 2021 a.out.h
lrwxrwxrwx.  1 root root     25 923 15:56 apache -> /usr/local/apache/include  // 已经连接了
[root@localhost ~]# vim /etc/man_db.conf
MANDATORY_MANPATH                       /usr/man
MANDATORY_MANPATH                       /usr/share/man
MANDATORY_MANPATH                       /usr/local/share/man
MANDATORY_MANPATH                       /usr/local/apache/man // 加上这一条

[root@localhost ~]# vim /usr/local/apache/conf/httpd.conf
ServerName www.example.com:80   // 把这一行的注释取消掉

// 启动apache
[root@localhost ~]# apachectl start
[root@localhost ~]# ss -anlt
State     Recv-Q     Send-Q         Local Address:Port         Peer Address:Port    
LISTEN    0          128                  0.0.0.0:22                0.0.0.0:*       
LISTEN    0          128                        *:80                      *:*       
LISTEN    0          128                     [::]:22                   [::]:* 

// 设置开启自启
[root@localhost ~]# cat /usr/lib/systemd/system/sshd.service > /usr/lib/systemd/system/httpd.service
[root@localhost ~]# vim /usr/lib/systemd/system/httpd.service
[Unit]
Description=Httpd server daemon
After=network.target 

[Service]
Type=forking
ExecStart=/usr/local/apache/bin/apachectl start
ExecStop=/usr/local/apache/bin/apachectl stop
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl enable --now httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service/usr/lib/systemd/system/httpd.service.
[root@localhost ~]# ss -anlt
State     Recv-Q     Send-Q         Local Address:Port         Peer Address:Port    
LISTEN    0          128                  0.0.0.0:22                0.0.0.0:*       
LISTEN    0          128                        *:80                      *:*       
LISTEN    0          128                     [::]:22                   [::]:*       
[root@localhost ~]# systemctl status httpdhttpd.service - Httpd server daemon
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: d>
   Active: active (running) since Thu 2021-09-23 16:08:41 CST; 20s ago
  Process: 184171 ExecStart=/usr/local/apache/bin/apachectl start (code=exited, sta>
 Main PID: 184184 (httpd)
    Tasks: 6 (limit: 23789)
   Memory: 4.9M
   CGroup: /system.slice/httpd.service
           ├─184184 /usr/local/apache/bin/httpd -k start
           ├─184188 /usr/local/apache/bin/httpd -k start
           ├─184189 /usr/local/apache/bin/httpd -k start
           ├─184190 /usr/local/apache/bin/httpd -k start
           ├─184191 /usr/local/apache/bin/httpd -k start
           └─184192 /usr/local/apache/bin/httpd -k start

923 16:08:41 localhost.localdomain systemd[1]: Starting Httpd server daemon...
923 16:08:41 localhost.localdomain systemd[1]: Started Httpd server daemon.

安装MySQL

// 安装依赖包
[root@localhost ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel

// 创建MySQL用户
[root@localhost ~]# useradd -r -M -s /sbin/nologin mysql
[root@localhost ~]# id mysql
uid=993(mysql) gid=990(mysql)=990(mysql)

// 下载软件包
[root@localhost src]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.34-linux-glibc2.12-x86_64.tar

// 解压到/usr/local/[root@localhost src]# tar xf mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz -C /usr/local/

//修改目录/usr/local/mysql的属主属组
[root@localhost src]# cd /usr/local/
[root@localhost local]# ls
apache    bin    include  libexec                              share
apr       etc    lib      mysql-5.7.34-linux-glibc2.12-x86_64  src
apr-util  games  lib64    sbin
[root@localhost local]# mv mysql-5.7.34-linux-glibc2.12-x86_64 mysql
[root@localhost local]# chown -R mysql.mysql mysql
[root@localhost local]# ll
总用量 0
drwxr-xr-x. 14 root  root  164 923 15:53 apache
drwxr-xr-x.  6 root  root   58 923 15:43 apr
drwxr-xr-x.  5 root  root   43 923 15:46 apr-util
drwxr-xr-x.  2 root  root    6 812 2018 bin
drwxr-xr-x.  2 root  root    6 812 2018 etc
drwxr-xr-x.  2 root  root    6 812 2018 games
drwxr-xr-x.  2 root  root    6 812 2018 include
drwxr-xr-x.  2 root  root    6 812 2018 lib
drwxr-xr-x.  2 root  root    6 812 2018 lib64
drwxr-xr-x.  2 root  root    6 812 2018 libexec
drwxr-xr-x.  9 mysql mysql 129 923 16:20 mysql
drwxr-xr-x.  2 root  root    6 812 2018 sbin
drwxr-xr-x.  5 root  root   49 77 08:44 share
drwxr-xr-x.  2 root  root    6 812 2018 src

// 配置环境变量
[root@localhost ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@localhost ~]# source /etc/profile.d/mysql.sh 
[root@localhost ~]# ls /usr/local/mysql/
bin  docs  include  lib  LICENSE  man  README  share  support-files
[root@localhost ~]# ln -s /usr/local/mysql/include /usr/include/mysql/
[root@localhost ~]# vim /etc/man_db.conf
MANDATORY_MANPATH                       /usr/man
MANDATORY_MANPATH                       /usr/share/man
MANDATORY_MANPATH                       /usr/local/share/man
MANDATORY_MANPATH                       /usr/local/apache/man
MANDATORY_MANPATH                       /usr/local/mysql/man  // 加上这一行
[root@localhost ~]# vim /etc/ld.so.conf.d/mysql.conf
[root@localhost ~]# cat /etc/ld.so.conf.d/mysql.conf
/usr/local/mysql/lib
[root@localhost ~]# ldconfig

// 建立数据存放目录
[root@localhost ~]# mkdir /opt/data
[root@localhost ~]# chown -R mysql.mysql /opt/data
[root@localhost ~]# ll /opt/
总用量 0
drwxr-xr-x. 2 mysql mysql 6 923 16:29 data

// 初始化数据库
[root@localhost ~]# mysqld --initialize-insecure --user mysql --datadir=/opt/data/
2021-09-23T08:31:44.036119Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-09-23T08:31:44.199363Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-09-23T08:31:44.230951Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-09-23T08:31:44.290888Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: af1efa80-1c48-11ec-8767-000c290e69ba.
2021-09-23T08:31:44.291750Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-09-23T08:31:44.732517Z 0 [Warning] CA certificate ca.pem is self signed.
2021-09-23T08:31:44.785672Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.

// 生成配置文件
[root@localhost ~]# cat > /etc/my.cnf <<EOF
> [mysqld]
> basedir = /usr/local/mysql
> datadir = /opt/data
> socket = /tmp/mysql.sock
> port = 3306
> pid-file = /opt/data/mysql.pid
> user = mysql
> skip-name-resolve
> EOF

// 创建service文件,设置开启自启
[root@localhost ~]# cp /usr/lib/systemd/system/httpd.service /usr/lib/systemd/system/mysqld.service
[root@localhost ~]# vim /usr/lib/systemd/system/mysqld.service 
[root@localhost ~]# cat /usr/lib/systemd/system/mysqld.service 
[Unit]
Description=Mysql server daemon
After=network.target 

[Service]
Type=forking
ExecStart=/usr/local/mysql/support-files/mysql.service start
ExecStop=/usr/local/mysql/support-files/mysql.service stop
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target


[root@localhost ~]# vim /usr/local/mysql/support-files/mysql.server
basedir=/usr/local/mysql
datadir=/opt/data

[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl enable --now mysqld
[root@localhost ~]# ss -anlt
State                 Recv-Q                 Send-Q                                 Local Address:Port                                 Peer Address:Port                
LISTEN                0                      128                                          0.0.0.0:22                                        0.0.0.0:*                   
LISTEN                0                      128                                                *:80                                              *:*                   
LISTEN                0                      128                                             [::]:22                                           [::]:*                   
LISTEN                0                      80                                                 *:3306                                            *:*   

// 设置数据库密码
[root@localhost ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.34 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> set password = password("syb123");
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> quit
Bye
[root@localhost ~]# mysql -uroot -psyb123
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.34 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> quit
Bye

PHP安装

// 安装epel[root@localhost ~]# yum -y install epel-release

// 下载软件包
[root@localhost src]# wget https://www.php.net/distributions/php-8.0.10.tar.gz

// 解压
[root@localhost src]# tar xf php-8.0.10.tar.gz

// 安装依赖包
[root@localhost php-8.0.10]# yum -y install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libicu-devel libjpeg libjpeg-devel libpng libpng-devel openldap-devel  pcre-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel mhash mhash-devel php-mysqlnd
[root@localhost php-8.0.10]# yum -y install libsqlite3x-devel
[root@localhost php-8.0.10]# yum -y install http://mirror.centos.org/centos/8-stream/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm
[root@localhost php-8.0.10]# yum -y install libzip-devel

// 编译安装PHP
[root@localhost php-8.0.10]# ./configure --prefix=/usr/local/php8  \
--with-config-file-path=/etc \
--enable-fpm \
--disable-debug \
--disable-rpath \
--enable-shared \
--enable-soap \
--with-openssl \
--enable-bcmath \
--with-iconv \
--with-bz2 \
--enable-calendar \
--with-curl \
--enable-exif  \
--enable-ftp \
--enable-gd \
--with-jpeg \
--with-zlib-dir \
--with-freetype \
--with-gettext \
--enable-mbstring \
--enable-pdo \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-readline \
--enable-shmop \
--enable-simplexml \
--enable-sockets \
--with-zip \
--enable-mysqlnd-compression-support \
--with-pear \
--enable-pcntl \
--enable-posix
[root@localhost php-8.0.10]# make && make install

// 环境变量
[root@localhost php-8.0.10]# echo 'export PATH=/usr/local/php8/bin:$PATH' > /etc/profile.d/php.sh
[root@localhost php-8.0.10]# source /etc/profile.d/php.sh
[root@localhost apache]# which php
/usr/local/php8/bin/php

//配置php-fpm
[root@localhost php-8.0.10]# cp php.ini-production /etc/php.ini
[root@localhost php-8.0.10]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@localhost php-8.0.10]# chmod +x /etc/init.d/php-fpm 
[root@localhost etc]# cp php-fpm.conf.default php-fpm.conf
[root@localhost etc]# ls
pear.conf  php-fpm.conf  php-fpm.conf.default  php-fpm.d
[root@localhost php-fpm.d]# cp www.conf.default www.conf
[root@localhost php-fpm.d]# ls
www.conf  www.conf.default

// 启动PHP服务
[root@localhost php-fpm.d]# service php-fpm start
Starting php-fpm  done
[root@localhost php-fpm.d]# ss -anlt
State     Recv-Q    Send-Q         Local Address:Port         Peer Address:Port    
LISTEN    0         128                  0.0.0.0:22                0.0.0.0:*       
LISTEN    0         128                127.0.0.1:9000              0.0.0.0:*       
LISTEN    0         128                        *:80                      *:*       
LISTEN    0         128                     [::]:22                   [::]:*       
LISTEN    0         80                         *:3306                    *:*

配置Apache

// 取消这两行的注释
[root@localhost php-8.0.10]# vim /usr/local/apache/conf/httpd.conf
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so


配置虚拟主机

[root@localhost php-8.0.10]# cd /usr/local/apache/
[root@localhost apache]# ls
bin    cgi-bin  error   icons    logs  manual
build  conf     htdocs  include  man   modules
[root@localhost apache]# ls htdocs/
index.html
[root@localhost apache]# mkdir htdocs/test
[root@localhost apache]# vim htdocs/test/index.php
 php
   phpinfo();
?>
[root@localhost apache]# chown -R apache.apache /usr/local/apache/htdocs/

[root@localhost apache]# vim conf/httpd.conf
<VirtualHost *:80>
    DocumentRoot "/usr/local/apache/htdocs/test"
    ServerName www.shaoyaobang.com
    ProxyRequests Off
    ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/usr/local/apache/htdocs/test/$1
    <Directory "/usr/local/apache/htdocs/test">
        Options none
        AllowOverride none
        Require all granted
    Directory>
VirtualHost>

    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz
    AddType application/x-httpd-php .php   // 添加这行
    AddType application/x-httpd-php-source .phps    // 添加这行

<IfModule dir_module>
    DirectoryIndex index.php  index.html  //index.html前面加上index.php
IfModule>

// 重启httpd
[root@localhost ~]# systemctl restart httpd
[root@localhost ~]# 

验证

部署LAMP_第1张图片

你可能感兴趣的:(Linux,linux,vim,运维)