在CENTOS上源码搭建LNMP环境

洼地云 tuoyidashi.png

前言

1.操作前提:

  • CentOS Linux release 7.5.1804;
  • sudo用户(需要root权限);

2.需要安装的组件:

  • nginx稳定版:nginx-1.14.0;
  • MariaDB 10.3.10 Stable;
  • PHP 7.2.11 Stable;

3.操作步骤:

  • 添加环境依赖包;
  • 安装libiconv,libmcrypt等;
  • 安装nginx;
  • 安装php;
  • 安装mariadb;

下载安装包

wget http://nginx.org/download/nginx-1.14.0.tar.gz &&  tar xzvf nginx-1.14.0.tar.gz 

wget http://am1.php.net/distributions/php-7.2.11.tar.gz && tar xzvf php-7.2.11.tar.gz

wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz &&  tar xzvf libiconv-1.14.tar.gz

wget http://sourceforge.net/projects/mcrypt/files/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz && tar xzvf ./libmcrypt-2.5.8.tar.gz

删除不需要的源码包:

rm -rf *.tar.gz

添加环境依赖包

以下依赖包是整个Lnmp搭建过程中所需的基本依赖包,如果在安装过程中出现错误,提示缺少某依赖包,可以自己安装:

#执行下面这条命令,各参数之间以空格分开

sudo yum -y install zlib-devel pcre-devel openssl-devel gcc gcc-c++  ncurses-devel perl perl-devel perl-ExtUtils-Embedlibjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel  libxml2 libxml2-devel curl-devel libxslt libxslt-devel gd gd-devel GeoIP GeoIP-devel

编译nginx

为了使Nginx能够启用http/2,需要通过源码的方式安装nginx,并指定–with-openssl的版本在1.0.2版本以上,这里我将需要版本的依赖安装在/usr/local/src目录:

$ cd /usr/local/src/

# PCRE version 8.40
$ sudo wget https://ftp.pcre.org/pub/pcre/pcre-8.40.tar.gz && sudo tar xzvf pcre-8.40.tar.gz

# zlib version 1.2.11
$ sudo wget https://www.zlib.net/zlib-1.2.11.tar.gz && sudo tar xzvf zlib-1.2.11.tar.gz

# OpenSSL version openssl-1.1.0h
$ sudo wget https://www.openssl.org/source/openssl-1.1.0h.tar.gz && sudo tar xzvf openssl-1.1.0h.tar.gz

删除不需要的包

sudo rm -rf *.tar.gz

编译nginx

cd ~/src/nginx-1.14.0
sudo ./configure --prefix=/etc/nginx \
            --sbin-path=/usr/sbin/nginx \
            --modules-path=/usr/lib64/nginx/modules \
            --conf-path=/etc/nginx/nginx.conf \
            --error-log-path=/var/log/nginx/error.log \
            --pid-path=/var/run/nginx.pid \
            --lock-path=/var/run/nginx.lock \
            --user=nginx \
            --group=nginx \
            --build=CentOS \
            --builddir=nginx-1.14.0 \
            --with-select_module \
            --with-poll_module \
            --with-threads \
            --with-file-aio \
            --with-http_ssl_module \
            --with-http_v2_module \
            --with-http_realip_module \
            --with-http_addition_module \
            --with-http_xslt_module=dynamic \
            --with-http_image_filter_module=dynamic \
            --with-http_geoip_module=dynamic \
            --with-http_sub_module \
            --with-http_dav_module \
            --with-http_flv_module \
            --with-http_mp4_module \
            --with-http_gunzip_module \
            --with-http_gzip_static_module \
            --with-http_auth_request_module \
            --with-http_random_index_module \
            --with-http_secure_link_module \
            --with-http_degradation_module \
            --with-http_slice_module \
            --with-http_stub_status_module \
            --http-log-path=/var/log/nginx/access.log \
            --http-client-body-temp-path=/var/cache/nginx/client_temp \
            --http-proxy-temp-path=/var/cache/nginx/proxy_temp \
            --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
            --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
            --http-scgi-temp-path=/var/cache/nginx/scgi_temp \
            --with-mail=dynamic \
            --with-mail_ssl_module \
            --with-stream=dynamic \
            --with-stream_ssl_module \
            --with-stream_realip_module \
            --with-stream_geoip_module=dynamic \
            --with-stream_ssl_preread_module \
            --with-compat \
            --with-pcre=/usr/local/src/pcre-8.40 \
            --with-pcre-jit \
            --with-zlib=/usr/local/src/zlib-1.2.11 \
            --with-openssl=/usr/local/src/openssl-1.1.0h \
            --with-openssl-opt=no-nextprotoneg \
            --with-debug

上面--prefix=/etc/nginx为安装目录。

sudo make && sudo make install

创建软链

sudo ln -s /usr/lib64/nginx/modules /etc/nginx/modules

这样就可以在nginx的配置文件中像这样加载动态模块:load_module modules/ngx_foo_module.so;。

查看编译信息

sudo nginx -V

修改/etc/nginx/nginx.conf

去掉 user nobody;一行的注释。

检查配置是否存在潜在错误

## 执行检查可能会报错
sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: [emerg] mkdir() "/var/cache/nginx/client_temp" failed (2: No such file or directory)
nginx: configuration file /etc/nginx/nginx.conf test failed

## 如果报上面的错误,则执行下面的代码
sudo mkdir -p /var/cache/nginx && sudo nginx -t

创建nginx系统服务单元

编辑文件:

sudo vi /usr/lib/systemd/system/nginx.service

将下面的内容复制进去

[Unit]
Description=nginx - high performance web server
Documentation=https://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID

[Install]
WantedBy=multi-user.target

启动nginx并设置开机启动

sudo systemctl start nginx.service && sudo systemctl enable nginx.service

至此,nginx的安装到此结束,nginx的源码安装参考出处:centos7通过编译源码的方式安装nginx

接下来进行源码编译安装Php的操作。

编译安装php

php需要libiconv、libmcrypt等依赖,下面进行编译:

编译libiconv

cd ~/src/libiconv-1.14
./configure --prefix=/usr/local/libiconv-1.14
cd srclib
sed -i -e '/gets is a security/d' ./stdio.in.h
cd ..
sudo make -j2 && sudo make install

编译libmcrypt

cd ~/src/libmcrypt-2.5.8
./configure --prefix=/usr/local/libmcrypt-2.5.8
sudo make -j2 && sudo make install

编译php

cd ~/src/php-7.2.11

sudo ./configure --prefix=/usr/local/php-7.2.11 --with-config-file-path=/usr/local/php-7.2.11/etc --with-mysqli=mysqlnd --with-iconv-dir=/usr/local/libiconv-1.14 --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --enable-bcmath --enable-inline-optimization --with-curl --with-fpm-user=michael --with-fpm-group=www --enable-mbstring --with-mcrypt=/usr/local/libmcrypt-2.5.8 --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-sockets --enable-zip --enable-ftp --without-pear --enable-pdo --with-pdo-mysql=mysqlnd --disable-fileinfo --with-xmlrpc --enable-opcache --enable-fpm --enable-exif

注意:以上各参数以空格隔开,–prefix指定你想要设置的安装目录,–with-iconv-dir指定你前面libiconv的安装目录,–with-fpm-user指定你的用户,–with-fpm-group指定你的用户组,–with-mcrypt需要指定你libmcrypt的安装目录,不然编译PHP时,就算安装了也会提示找不到libmcrypt,请重新安装的错误!

执行安装

sudo make -j2
sudo make test
sudo make install

配置php

1)创建配置文件

根据你configure指定的配置文件目录(–with-config-file-path),我需要将源码目录下的php.ini-production文件复制一份到配置文件目录:

cp ~/src/php-7.2.11/php.ini-development /usr/local/php-7.2.11/etc/php.ini

2)将安装目录/usr/local/php-7.2.11/etc下的php-fpm.conf.default文件复制一份并重名为php-fpm.conf

cp php-fpm.conf.default  php-fpm.conf

3)做一个软链接

ln -s /usr/local/php-7.2.11/bin/php /usr/bin/

4)进入安装目录/usr/local/php-7.2.11/etc/php-fpm.d目录下,将www.conf.default复制一份并重命名为www.conf

cp www.conf.default  www.conf

5)配置php-fpm.conf文件

vi /usr/local/php-7.2.11/etc/php-fpm.conf

基本不变。

6)配置www.conf文件

vi /usr/local/php-7.2.11/etc/php-fpm.d/www.conf

基本不变。

7)设置启动项

编辑文件

sudo vi /usr/lib/systemd/system/php-fpm.service

复制以下内容到上面的文件:

[Unit]
Description=php-fpm
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/php-7.2.11/sbin/php-fpm
PrivateTmp=true
[Install]
WantedBy=multi-user.target

8)启动php-fpm

# 启动
sudo systemctl start php-fpm.service

# 设置开机自启
sudo systemctl enable php-fpm.service

#查看运行状态
sudo systemctl status php-fpm.service

不详尽处请参考php的源码安装步骤参考文章:CentOS 7使用源码安装php

安装Mariadb

mariadb采用官方推荐的yum的方式安装,编辑文件:

/etc/yum.repos.d/MariaDB.repo

复制下面内容到文件中:

# MariaDB 10.3 CentOS repository list - created 2018-10-30 13:52 UTC
# http://downloads.mariadb.org/mariadb/repositories/
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.3/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

执行安装命令:

sudo yum install MariaDB-server MariaDB-client

启动服务:

#立刻启动
sudo systemctl start mariadb

#开机启动
sudo systemctl enable mariadb 

#查看服务状态
sudo systemctl status mariadb 

设置root密码:

/usr/bin/mysqladmin -u root password '1234567890'

通过修改mysql.user表的方式重新修改一下root密码,并配置实现远程连接:

[michael@dg-vps php-fpm.d]$ mysql -u root -p
Enter password: 输入你刚刚设置的密码

MariaDB [(none)]> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [mysql]> UPDATE user SET password=PASSWORD('你的密码') WHERE User='root';

#为root数据库用户添加本地访问所有数据库的权限
MariaDB [mysql]> grant all privileges  on *.* to root@'localhost' identified by "1234567890";
Query OK, 0 rows affected (0.00 sec)

#为root数据库用户添加远程连接访问所有数据库的权限
MariaDB [mysql]> grant all privileges  on *.* to root@'%' identified by "1234567890";
Query OK, 0 rows affected (0.00 sec)

#最后刷新权限
MariaDB [mysql]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

至此,mariadb安装完毕。

不详尽处请参考:CentOS使用yum安装mariadb

赞(0)
未经允许禁止转载:优米格 » 在CENTOS上源码搭建LNMP环境

评论 抢沙发

合作&反馈&投稿

商务合作、问题反馈、投稿,欢迎联系

广告合作侵权联系

登录

找回密码

注册