Centos7下编译安装lnmp环境(nginx1.18.0+mysql5.7.44+php7.4.33)

centos7编译安装mysql5.7.44+nginx1.18.0+php7.4.33,和之前的教程大体一样,只是有一些微改变,编译安装顺序最好按此文的顺序mysql->nginx->php这样
可以看看我之前centos6编译的教程,https://sulao.cn/post/111.html

首先约定下部署规范,所有软件均上传到/usr/loca/src目录,编译安装到/usr/local/下的软件名目录

下面我们来看看centos7上编译的过程

首先关闭selinux

sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
setenforce 0

关闭防火墙并停用开机启动

systemctl stop firewalld.service
systemctl disable firewalld.service

更换阿里云yum源

yum install wget -y
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo_bak
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

更新yum缓存

yum makecache

安装依赖包

yum install -y make apr* autoconf automake curl-devel gcc gcc-c++ zlib-devel openssl openssl-devel pcre-devel libjpeg-devel libpng-devel zlib-devel libXpm* freetype php-common ncurses* libtool* libxml2 libxml2-devel patch freetype-devel cmake pcre sqlite-devel

安装mysql5.7

groupadd mysql  #添加mysql组    
useradd -g mysql mysql -s /sbin/nologin  #创建用户mysql并加入到mysql组,不允许mysql用户直接登录系统    
mkdir -p /data/mysql  #创建MySQL数据库存放目录    
chown -R mysql:mysql /data/mysql   #设置MySQL数据库目录权限    
mkdir -p /usr/local/mysql #创建MySQL安装目录    
cd /usr/local/src    
tar zxvf mysql-5.7.44.tar.gz  #解压    
cd mysql-mysql-5.7.44    
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql  -DMYSQL_DATADIR=/data/mysql  -DSYSCONFDIR=/etc -DWITH_BOOST=boost -DDOWNLOAD_BOOST=1  #DDOWNLOAD_BOOST参数会联网下载boost,如何下载带boost header的版本的mysql则不用添加此参数   
make #编译    
make install  #安装

添加my.cnf配置文件,如果/etc下没有就新增,有就清空内容添加以下内容 

#vi /etc/my.cnf
[client]
port = 3306
socket = /data/mysql/mysql.sock
default-character-set = utf8mb4
[mysql]
prompt="MySQL [\d]> "
no-auto-rehash
[mysqld]
port = 3306
socket = /data/mysql/mysql.sock
basedir = /usr/local/mysql
datadir = /data/mysql
pid-file = /data/mysql/mysql.pid
user = mysql
bind-address = 0.0.0.0
server-id = 1
init-connect = 'SET NAMES utf8mb4'
character-set-server = utf8mb4
skip-name-resolve
#skip-networking
back_log = 300
max_connections = 1000
max_connect_errors = 6000
open_files_limit = 65535
table_open_cache = 128
max_allowed_packet = 500M
binlog_cache_size = 1M
max_heap_table_size = 8M
tmp_table_size = 16M
read_buffer_size = 2M
read_rnd_buffer_size = 8M
sort_buffer_size = 8M
join_buffer_size = 8M
key_buffer_size = 4M
thread_cache_size = 8
query_cache_type = 1
query_cache_size = 8M
query_cache_limit = 2M
ft_min_word_len = 4
log_bin = mysql-bin
binlog_format = mixed
expire_logs_days = 7
log_error = /data/mysql/mysql-error.log
slow_query_log = 1
long_query_time = 1
slow_query_log_file = /data/mysql/mysql-slow.log
performance_schema = 0
#lower_case_table_names = 1
skip-external-locking
default_storage_engine = InnoDB
innodb_file_per_table = 1
innodb_open_files = 500
innodb_buffer_pool_size = 64M
innodb_write_io_threads = 4
innodb_read_io_threads = 4
innodb_thread_concurrency = 0
innodb_purge_threads = 1
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 2M
innodb_log_file_size = 32M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
bulk_insert_buffer_size = 8M
myisam_sort_buffer_size = 8M
myisam_max_sort_file_size = 10G
interactive_timeout = 28800
wait_timeout = 28800
[mysqldump]
quick
max_allowed_packet = 500M
[myisamchk]
key_buffer_size = 8M
sort_buffer_size = 8M
read_buffer = 4M
write_buffer = 4M

:wq!  #保存退出
初始化mysql5.7数据库的方式跟mysql5.6和5.5不一样了,这里要注意

/usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql   
cp ./support-files/mysql.server  /usr/local/mysql/bin/mysql.server  #把Mysql启停脚本放置到bin目录下,方便后续编写系统服务   
#vi /usr/local/mysql/bin/mysql.server  #编辑    
basedir = /usr/local/mysql   #MySQL程序安装路径    
datadir = /data/mysql  #MySQl数据库存放目录

:wq! 保存退出

chmod +x /usr/local/mysql/bin/mysql.server

mysql开机启动配置

vi /usr/lib/systemd/system/mysqld.service
[Unit]
Description=mysql project
After=mysqld.service
  
[Service]
Type=forking
User=mysql
Group=mysql
PIDFile=/data/mysql/mysql.pid
ExecStart=/usr/local/mysql/bin/mysql.server start
ExecReload=/usr/local/mysql/bin/mysql.server restart
ExecStop=/usr/local/mysql/bin/mysql.server stop
PrivateTmp=true
  
[Install]
WantedBy=multi-user.target

给启动服务脚本754权限

chmod 754 /usr/lib/systemd/system/mysqld.service
systemctl start mysqld.service #启动mysql
#如果启动报错重新加载下daemon
systemctl daemon-reload
systemctl enable mysqld.service #添加到开机启动

把mysql服务加入系统环境变量:在最后添加下面这一行

vi /etc/profile    
export PATH=$PATH:/usr/local/mysql/bin

:wq! #保存退出

使其生效

source /etc/profile

修改mysql密码,默认密码为空

mysql -u root -p #直接回车登陆
SET PASSWORD=PASSWORD("admin")

nginx安装和之前一样,主要也是开机启动服务需要自己编写,我们可以在这里下载nginx:https://nginx.org/en/download.html

groupadd  www  #添加www组    
useradd -g  www www -s /sbin/nologin  #创建nginx运行账户www并加入到www组,不允许www用户直接登录系统 
tar -zxvf nginx-1.18.0.tar.gz
cd nginx-1.18.0
./configure --prefix=/usr/local/nginx --without-http_memcached_module --user=www --group=www --with-http_stub_status_module --without-http-cache --with-http_ssl_module --with-http_realip_module --with-pcre
make #编译    
make install  #安装

nginx开机启动

vi /usr/lib/systemd/system/nginxd.service
[Unit]
Description=nginx project
After=nginxd.service
  
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true
  
[Install]
WantedBy=multi-user.target

给开机服务脚本754权限

chmod 754 /usr/lib/systemd/system/nginxd.service
systemctl start nginxd.service
#如果启动报错重新加载下daemon
systemctl daemon-reload
systemctl enable nginxd.service

安装libmcrypt,

cd /usr/local/src
wget http://nchc.dl.sourceforge.net/project/mcrypt/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz  
tar zxvf libmcrypt-2.5.8.tar.gz
cd  libmcrypt-2.5.8
./configure
make
make install

编译安装php7的时候会报错,我踩过坑了

configure: error: Please reinstall the libzip distribution

需要安装高版本的lizip,如果安装1.5的版本会提示cmake版本低,我这里方便还是使用的1.3.2的版本

yum remove libzip
wget https://libzip.org/download/libzip-1.3.2.tar.gz
tar -zxvf libzip-1.3.2.tar.gz
cd libzip-1.3.2
./configure
make && make install
#设置pkg_config_path环境变量
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig

如果再次编译php7报以下错误

/usr/local/include/zip.h:59:21: fatal error: zipconf.h: No such file or directory

可以尝试手工复制一个过去

cp /usr/local/lib/libzip/include/zipconf.h /usr/local/include/zipconf.h

再次编译php7

mkdir -p /usr/local/php7
ln -s /usr/lib64/liblber* /usr/lib/ #ldap模块需要的库
tar -zxvf php-7.4.33.tar.gz
cd php-7.4.33
./configure --prefix=/usr/local/php7 --with-config-file-path=/usr/local/php7/etc --with-mysqli=/usr/local/mysql/bin/mysql_config --with-mysql-sock=/tmp/mysql.sock --with-pdo-mysql=/usr/local/mysql --enable-mysqlnd --enable-gd --with-iconv --with-ldap=shared --with-zlib --enable-xml --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --enable-mbregex  --enable-fpm --enable-mbstring --disable-mbregex --enable-ftp --with-openssl --enable-pcntl --enable-sockets --with-xmlrpc --with-zip --enable-soap --without-pear --with-gettext --enable-session --with-curl --with-jpeg --with-freetype --enable-opcache
make   #编译    
make install    #安装

如果内存小于1G会报错:make: *** [ext/fileinfo/libmagic/apprentice.lo] Error 1
在./configure添加:--disable-fileinfo   即可

cp  php.ini-production   /usr/local/php7/etc/php.ini  #复制php配置文件到安装目录    
rm -rf /etc/php.ini   #删除系统自带配置文件    
ln -s /usr/local/php7/etc/php.ini  /etc/php.ini    #添加软链接    
cp  /usr/local/php7/etc/php-fpm.conf.default   /usr/local/php7/etc/php-fpm.conf      #拷贝模板文件为php-fpm配置文件
cd /usr/local/php7/etc/php-fpm.d/
cp www.conf.default www.conf
vi  www.conf
user = www    #设置php-fpm运行账号为www    
group = www   #设置php-fpm运行组为www   
wq! #保存退出
vi  /usr/local/php7/etc/php-fpm.conf  #编辑    
pid = run/php-fpm.pid    #取消前面的分号 
wq! #保存退出
cp sapi/fpm/init.d.php-fpm /usr/local/php7/bin/php-fpm  #编写php开机启动需要的文件
chmod +x /usr/local/php7/bin/php-fpm  #添加执行权限

php开机启动配置

vi /usr/lib/systemd/system/phpd.service
[Unit]
Description=php project
After=phpd.service
  
[Service]
Type=forking
PIDFile=/usr/local/php7/var/run/php-fpm.pid
ExecStart=/usr/local/php7/bin/php-fpm start
ExecReload=/usr/local/php7/bin/php-fpm restart
ExecStop=/usr/local/php7/bin/php-fpm stop
PrivateTmp=true
  
[Install]
WantedBy=multi-user.target

给开机启动服务脚本添加754权限

chmod 754 /usr/lib/systemd/system/phpd.service
systemctl start phpd.service
#如果启动报错重新加载下daemon
systemctl daemon-reload
systemctl enable phpd.service

我们也把php执行文件加到环境变量吧

vi /etc/profile    
export PATH=$PATH:/usr/local/php7/bin

:wq! #保存退出

使环境变量生效

source /etc/profile

这样所有需要的软件都已经编译完,我们可以重新启动下服务器看看nginx,php,mysql服务是不是自动启动,他们分别对应的监听端口是

php -> 9000
mysql -> 3306
nginx -> 80

{DAC164F7-6F6E-4C9F-8AA9-754299947E8B}_20191018134649.jpg

接着我们配置网站,修改/usr/local/nginx/conf/nginx.conf配置文件,内容可以全部参照以下配置进行修改

cat /usr/local/nginx/conf/nginx.conf

user  www www;
worker_processes  auto;
error_log /var/log/nginx/error.log;
pid        logs/nginx.pid;

events {
    worker_connections  1024;
    multi_accept on; 
    use epoll; 
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  logs/access.log  main;

    client_header_timeout 30; 
    client_body_timeout 30;
    client_max_body_size 128m;
    reset_timedout_connection on;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  30;
    
    send_timeout 30;
    limit_conn_zone $binary_remote_addr zone=addr:5m; 
    limit_conn addr 100;

    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;

    fastcgi_buffer_size 128k;
    fastcgi_buffers 4 256k;
    fastcgi_busy_buffers_size 256k;

    gzip on;
    gzip_min_length 1k;
    gzip_buffers 16 64k;
    gzip_http_version 1.1;
    gzip_comp_level 9;
    gzip_types text/plain application/x-javascript text/css application/xml application/x-httpd-php;
    gzip_vary on;

    proxy_buffer_size 128k;
    proxy_buffers 32 32k;
    proxy_busy_buffers_size 128k;

    open_file_cache max=100000 inactive=30s; 
    open_file_cache_valid 60s; 
    open_file_cache_min_uses 2; 
    open_file_cache_errors on;

include vhost/*.conf;

}

接着我们在/usr/local/nginx/conf目录下创建vhost虚拟主机目录用来进行虚拟主机的配置,这样的好处就是每个虚拟主机(web网站)单独一个配置来管理自己的web相关配置和优化。

mkdir /usr/local/nginx/conf/vhost
vi /usr/local/nginx/conf/vhost/sulao.cn.conf #以下是我博客的配置为例
server {
    listen 80;
    server_name www.sulao.cn sulao.cn;
    root  /data/www/sulao.cn;
    index index.html index.php;
    # include /data/www/sulao.cn/.htaccess; #载入伪静态的配置文件
    location ~ .*\.(jpg|jpeg|png|gif|js|css)$ {
        expires 1d;
        valid_referers none blocked sulao.cn *.sulao.cn *.google.com *.baidu.com *.so.com *.haosou.com *.bing.com *.sm.cn;
        if ($invalid_referer) {
            return 404;
        }
    }
    location ~ \.php(/|$) {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_split_path_info ^(.+\.php)(.*)$;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}

如果是需要https访问,需要提前准备CA证书,那么虚拟主机的配置可以参考以下

server {
    listen 443;
    server_name sulao.cn;
    ssl on;
    ssl_certificate   /data/www/sulao.cn/sslkey/sulao.cn.pem;
    ssl_certificate_key  /data/www/sulao.cn/sslkey/sulao.cn.key;
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4:!DH:!DHE;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    root  /data/www/sulao.cn;
    index index.html index.php;
    # include /data/www/sulao.cn/.htaccess;  #载入伪静态的配置文件
    location ~ .*\.(jpg|jpeg|png|gif|js|css)$ {
        expires 1d;
        valid_referers none blocked sulao.cn *.sulao.cn *.google.com *.baidu.com *.so.com *.haosou.com *.bing.com *.sm.cn;
        if ($invalid_referer) {
            return 404;
        }
    }
    location ~ \.php(/|$) {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_split_path_info ^(.+\.php)(.*)$;
         fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}


内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://sulao.cn/post/728.html

我要评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。