Cấu hình môi trường LAMP từ mã nguồn

Để xây dựng hệ thống LAMP (Linux, Apache, MySQL, PHP) từ mã nguồn, bạn cần chuẩn bị các gói phần mềm sau cùng phiên bản phù hợp:

1. Chuẩn bị môi trường biên dịch

Cài đặt các công cụ và thư viện phát triển cần thiết:

yum -y install make gcc gcc-c++ openssl-devel expat-devel cmake bison ncurses-devel

2. Biên dịch và cài đặt APR & APR-Util

tar zxf apr-1.6.2.tar.gz -C /usr/local/src/
cd /usr/local/src/apr-1.6.2/
./configure --prefix=/opt/apr
make -j4 && make install

tar zxf apr-util-1.6.0.tar.gz -C /usr/local/src/
cd /usr/local/src/apr-util-1.6.0/
./configure --prefix=/opt/apr-util --with-apr=/opt/apr/bin/apr-1-config
make -j4 && make install

3. Biên dịch PCRE

tar zxf pcre-8.41.tar.gz -C /usr/local/src/
cd /usr/local/src/pcre-8.41/
./configure --prefix=/opt/pcre
make -j4 && make install

4. Cài đặt Apache HTTP Server

tar zxf httpd-2.4.37.tar.gz -C /usr/local/src/
cd /usr/local/src/httpd-2.4.37/
./configure \
  --prefix=/opt/apache \
  --enable-so \
  --enable-rewrite \
  --enable-ssl \
  --with-apr=/opt/apr \
  --with-apr-util=/opt/apr-util \
  --with-pcre=/opt/pcre \
  --enable-modules=most \
  --enable-mpms-shared=all \
  --with-mpm=event

make -j4 && make install

Tạo service systemd để quản lý Apache:

cp /opt/apache/bin/apachectl /etc/init.d/httpd
chmod +x /etc/init.d/httpd

cat > /usr/lib/systemd/system/httpd.service <<EOF
[Unit]
Description=Apache Web Server
After=network.target

[Service]
Type=forking
ExecStart=/etc/init.d/httpd start
ExecReload=/etc/init.d/httpd restart
ExecStop=/etc/init.d/httpd stop
User=apache
Group=apache

[Install]
WantedBy=multi-user.target
EOF

useradd -r -s /sbin/nologin apache
chown -R apache:apache /opt/apache

systemctl daemon-reload
systemctl enable httpd --now
</code>

5. Biên dịch MySQL từ mã nguồn

Gỡ bỏ phiên bản mặc định và cài đặt phụ thuộc:

yum remove -y mariadb-libs mysql-libs boost-system
useradd -r -s /bin/false -M mysql
mkdir -p /opt/mysql/data
chown -R mysql:mysql /opt/mysql

Biên dịch MySQL với CMake:

tar zxf mysql-5.7.19.tar.gz -C /usr/local/src/
cd /usr/local/src/mysql-5.7.19/

cmake . \
  -DCMAKE_INSTALL_PREFIX=/opt/mysql \
  -DMYSQL_DATADIR=/opt/mysql/data \
  -DSYSCONFDIR=/etc \
  -DWITH_INNOBASE_STORAGE_ENGINE=1 \
  -DWITH_PARTITION_STORAGE_ENGINE=1 \
  -DMYSQL_UNIX_ADDR=/opt/mysql/mysql.sock \
  -DDEFAULT_CHARSET=utf8 \
  -DDEFAULT_COLLATION=utf8_general_ci \
  -DWITH_BOOST=/usr/local/src/boost_1_59_0

make -j4 && make install

Cấu hình MySQL:

cat > /etc/my.cnf <<EOF
[mysqld]
basedir=/opt/mysql
datadir=/opt/mysql/data
socket=/opt/mysql/mysql.sock
port=3306
character-set-server=utf8

[client]
socket=/opt/mysql/mysql.sock
EOF

echo 'export PATH=/opt/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
source /etc/profile.d/mysql.sh

cp /opt/mysql/support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
chkconfig --add mysqld

/opt/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/opt/mysql --datadir=/opt/mysql/data
systemctl start mysqld

mysql_secure_installation

6. Biên dịch PHP tích hợp Apache

yum -y install libxml2-devel curl-devel gd-devel libjpeg-devel libpng-devel freetype-devel

tar zxf php-7.1.24.tar.gz -C /usr/local/src/
cd /usr/local/src/php-7.1.24/

./configure \
  --prefix=/opt/php \
  --with-apxs2=/opt/apache/bin/apxs \
  --enable-mbstring \
  --with-curl \
  --with-gd \
  --enable-mysqlnd \
  --with-pdo-mysql=mysqlnd \
  --with-mysqli=mysqlnd \
  --with-mysql-sock=/opt/mysql/mysql.sock \
  --with-config-file-path=/opt/php/etc

make -j4 && make install
cp php.ini-production /opt/php/etc/php.ini

Kích hoạt PHP trong Apache:

echo "AddType application/x-httpd-php .php" >> /opt/apache/conf/httpd.conf
echo "AddType application/x-httpd-php-source .phps" >> /opt/apache/conf/httpd.conf

systemctl restart httpd

Tạo file kiểm tra:

echo "" > /opt/apache/htdocs/info.php

Truy cập http://your-server-ip/info.php để xác nhận cấu hình thành công.

Thẻ: apache mysql php linux source-compilation

Đăng vào ngày 8 tháng 6 lúc 23:44