分类目录归档:Linux

解决nginx+php/java/go/python+mysql下time_wait连接数过多问题

公司服务器连接数超过10K了,查了下大多数是没有即时回收,采用tcp复用容易后程序出现了故障,以前一直看到说PHP持久连接有问题,所以没怎么用,最近有机会试了下。

非常香,连接数直接降低到几百了,目前观察数周没有问题。

查看连接数

netstat -n |  wc -l # 总连接数
netstat -n | grep -i time_wait | wc -l # time_wait 连接数
netstat -anp # 查看占用端口过多的程序

tcp复用解决方案

网上大部分解决方案是修改sysctl.conf回收重用ipv4连接,但是可能带来其他问题

net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_timestamps=1
net.ipv4.tcp_tw_reuse=1
net.ipv4.tcp_tw_recycle=1
net.ipv4.tcp_fin_timeout=30
net.ipv4.tcp_keepalive_time = 600

nginx fastcgi(php) 解决方案

修改nginx.conf

upstream fastcgi_backend {
    server 127.0.0.1:9000;
    keepalive 60;
}

location ~ \.php$ {
        fastcgi_pass  fastcgi_backend;
        fastcgi_keep_conn on;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
}

主要是 fastcgi_keep_conn onupstreamkeepalive

nginx + proxy (python/go/java) 解决方案

upstream wxpic {
    keepalive 60;
    server 127.0.0.1:xxx;
}

server {
    keepalive_requests 10000; # 默认100
    location / {
        proxy_http_version 1.1;
        proxy_set_header Connection "";
    }
}

主要是 proxy_http_version, proxy_set_header Connection ""以及keepalive_requests

mysql数据库连接解决方案

修改yii数据库配置采用长链接

'db' => [ 
    'class' => 'yii\db\Connection',
    'dsn' => '*', 'username' => '*', 
    'password' => '*', 
    'charset' => 'utf8mb4', 
    'attributes' => [ 
       PDO::ATTR_PERSISTENT => true 
    ]
],

redis 连接数

php redis连接数过多解决办法(Yii,predis,phpredis等)

linux下PhpStorm,WebStorm,PyChram,IntelliJ IDEA,Android Studio使用ibus,fcitx输入中文

找到在pycharm安装目录bin下的pycharm.sh文件

在pycharm.sh文件中添加以下代码:

export GTK_IM_MODULE=ibus 
export QT_IM_MODULE=ibus 
export XMODIFIERS=@im=ibus

添加的位置

在Run the IDE之前,添加到文件末尾无效

CLASSPATH="$IDE_HOME/lib/bootstrap.jar"
CLASSPATH="$CLASSPATH:$IDE_HOME/lib/extensions.jar"
CLASSPATH="$CLASSPATH:$IDE_HOME/lib/util.jar"
CLASSPATH="$CLASSPATH:$IDE_HOME/lib/jdom.jar"
CLASSPATH="$CLASSPATH:$IDE_HOME/lib/log4j.jar"
CLASSPATH="$CLASSPATH:$IDE_HOME/lib/trove4j.jar"
CLASSPATH="$CLASSPATH:$IDE_HOME/lib/jna.jar"
if [ -n "$PYCHARM_CLASSPATH" ]; then
  CLASSPATH="$CLASSPATH:$PYCHARM_CLASSPATH"
fi

export GTK_IM_MODULE=ibus 
export QT_IM_MODULE=ibus 
export XMODIFIERS=@im=ibus

# ---------------------------------------------------------------------
# Run the IDE.
# ---------------------------------------------------------------------

如果使用fcitx输入法将ibus修改为fcitx即可

CentOS、Ubuntu使用samba实现文件共享

安装服务端

sudo yum  install -y samba samba-client ## centos
sudo apt install smbclient samba ## ubuntu

配置samba

[sambashare]
    comment = Samba on Ubuntu
    path = /home/username/sambashare
    read only = no
    browsable = yes

设置开机自动启动

sudo systemctl enable smb ## centos
sudo systemctl start smb ## centos
sudo systemctl enable smbd ## ubuntu
sudo systemctl start smbd ## ubuntu

添加当前用户到smb

sudo smbpasswd -a username

Windows访问samba

打开资源管理器,地址栏输入"\ip",然后输入密码即可

CentOS7通过yum安装PostgreSQL10

发现最新的postgresql版本

https://www.postgresql.org/download/linux/redhat/

下载

yum install https://download.postgresql.org/pub/repos/yum/10/redhat/rhel-7-x86_64/pgdg-centos10-10-2.noarch.rpm

安装

yum install postgresql10-server

设置数据目录

创建数据目录并修改权限

mkdir -p /data/postgresql/
chown postgres:postgres /data/postgresql/

复制开机启动文件

sudo cp /usr/lib/systemd/system/postgresql-10.service /etc/systemd/system/

修改开机启动文件

vim /etc/systemd/system/postgresql-10.service

找到

Environment=PGDATA=/var/lib/pgsql/10/data/

修改为

Environment=PGDATA=/data/postgresql/

初始化数据库

/usr/pgsql-10/bin/postgresql-10-setup initdb

开机自启动

systemctl enable postgresql-10
systemctl start postgresql-10
systemctl status postgresql-10

修改本地帐户权限

vim /data/postgresql/pg_hba.conf

host    all             all             127.0.0.1/32            ident

修改为

host    all             all             127.0.0.1/32            md5

重新加载配置

systemctl restart postgresql-10

新增用户和数据库

su - postgres
createuser dbuser
createdb -e -O dbuser dbname

设定密码

su - postgres
psql
\password dbuser (输入两次密码)

新用户登录数据库

psql -U dbuser -d dbname -h 127.0.0.1 (输入之前的密码)

允许非本机ip登录

  • 编辑data/postgresql.conf修改listen_addresses = 'localhost'listen_addresses = 'localhost'
  • 编辑data/pg_hba.conf

    host all all 192.168.1.0/24 md5

fedora27+python3+virtualenv+virtualenvwrapper安装及使用

安装virtualenvwrapper

这里使用pip3进行安装即可

sudo pip3 install virtualenvwrapper

设置virtualenvwrapper的运行环境变量

编辑~/.bashrc,加入以下几行

VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3 
export WORKON_HOME='~/.virtualenvs'
source /usr/local/bin/virtualenvwrapper.sh

使环境变量马上生效:

source ~/.bashrc

virtualenvwrapper使用

mkvirtualenv 新建虚拟环境

mkvirtualenv env1

建立后可以看到终端会以env1开头

(env1) [ning@localhost]$

再建立一个env2

mkvirtualenv env2

workon 启动/切换虚拟环境

workon env1

deactivate 离开虚拟环境

deactivate

rmvirtualenv 删除虚拟环境

rmvirtualenv env2

virtualenvwrapper help 查看virtualenvwrapper使用帮助

virtualenvwrapper is a set of extensions to Ian Bicking's virtualenv
tool.  The extensions include wrappers for creating and deleting
virtual environments and otherwise managing your development workflow,
making it easier to work on more than one project at a time without
introducing conflicts in their dependencies.

For more information please refer to the documentation:

    http://virtualenvwrapper.readthedocs.org/en/latest/command_ref.html

Commands available:

  add2virtualenv: add directory to the import path

  allvirtualenv: run a command in all virtualenvs

  cdproject: change directory to the active project

  cdsitepackages: change to the site-packages directory

  cdvirtualenv: change to the $VIRTUAL_ENV directory

  cpvirtualenv: duplicate the named virtualenv to make a new one

  lssitepackages: list contents of the site-packages directory

  lsvirtualenv: list virtualenvs

  mkproject: create a new project directory and its associated virtualenv

  mktmpenv: create a temporary virtualenv

  mkvirtualenv: Create a new virtualenv in $WORKON_HOME

  rmvirtualenv: Remove a virtualenv

  setvirtualenvproject: associate a project directory with a virtualenv

  showvirtualenv: show details of a single virtualenv

  toggleglobalsitepackages: turn access to global site-packages on/off

  virtualenvwrapper: show this help message

  wipeenv: remove all packages installed in the current virtualenv

  workon: list or change working virtualenvs

virtualenv –help 帮助

Usage: virtualenv [OPTIONS] DEST_DIR

Options:
  --version             show program's version number and exit
  -h, --help            show this help message and exit
  -v, --verbose         Increase verbosity.
  -q, --quiet           Decrease verbosity.
  -p PYTHON_EXE, --python=PYTHON_EXE
                        The Python interpreter to use, e.g.,
                        --python=python2.5 will use the python2.5 interpreter
                        to create the new environment.  The default is the
                        interpreter that virtualenv was installed with
                        (/usr/bin/python3)
  --clear               Clear out the non-root install and start from scratch.
  --no-site-packages    DEPRECATED. Retained only for backward compatibility.
                        Not having access to global site-packages is now the
                        default behavior.
  --system-site-packages
                        Give the virtual environment access to the global
                        site-packages.
  --always-copy         Always copy files rather than symlinking.
  --unzip-setuptools    Unzip Setuptools when installing it.
  --relocatable         Make an EXISTING virtualenv environment relocatable.
                        This fixes up scripts and makes all .pth files
                        relative.
  --no-setuptools       Do not install setuptools in the new virtualenv.
  --no-pip              Do not install pip in the new virtualenv.
  --no-wheel            Do not install wheel in the new virtualenv.
  --extra-search-dir=DIR
                        Directory to look for setuptools/pip distributions in.
                        This option can be used multiple times.
  --download            Download preinstalled packages from PyPI.
  --no-download, --never-download
                        Do not download preinstalled packages from PyPI.
  --prompt=PROMPT       Provides an alternative prompt prefix for this
                        environment.
  --setuptools          DEPRECATED. Retained only for backward compatibility.
                        This option has no effect.
  --distribute          DEPRECATED. Retained only for backward compatibility.
                        This option has no effect.

Vagrant box国内镜像及本地安装教程

Vagrant是非常好的本地开发环境搭建工具。

通常使用官方下载都会比较慢,而国内box下载地址较少,所以我特别下载了几个传到百度网盘。

(目前官方已支持CDN加速,可能不太需要了)

国内镜像下载

官方直接下载

官方已经采用cdn加速

vagrant box add ubuntu/trusty64
vagrant box add ubuntu/trusty32
vagrant box add generic/ubuntu1804
vagrant box add generic/ubuntu1604

vagrant box add generic/centos7
vagrant box add generic/centos8
vagrant box add generic/centos8

vagrant box add generic/debian8
vagrant box add generic/debian10

下载后的使用方法

添加vagrant box到box list

vagrant box add centos7 CentOS-7.box

初始化一个虚拟机使用刚才添加的vagrant box

vagrant init centos7

启动vagrant box虚拟机

vagrant up

使用Vagrant+CentOS 7搭建PHP7开发环境(含centos7.box直接下载地址)

Vagrant是一款基于命令行的虚拟机管理软件,可以用来快速部署统一的开发环境。

下载Vagrant

https://www.vagrantup.com/downloads.html

下载CentOS 7 Box

官方box下载地址

https://app.vagrantup.com/boxes/search

第三方box下载地址

http://www.vagrantbox.es/

使用原生下载

https://app.vagrantup.com/centos/boxes/7

vagrant init centos/7
vagrant up

vagrant 配置

  config.vm.network "public_network", ip: "192.168.31.245"
  config.vm.synced_folder "d:/data", "/data"
  config.vm.synced_folder "d:/phpcode", "/phpcode"

通过下载工具下载centos 7 box

官方box文件下载地址:http://cloud.centos.org/centos/7/vagrant/x86_64/images/

百度网盘box文件下载地址

  • CentOS 7: https://pan.baidu.com/s/1kVlAz59

添加并运行box

vagrant box add centos7 CentOS-7.box
vagrant init centos7
vagrant up

基础系统安装

基本系统安装

vagrant ssh
sudo passwd vagrant
sudo yum groupinstall "Development tools" -y
sudo yum install vim gcc kernel-devel kenel-devel-`uname -r`

禁止selinux

sudo setenforce 0 

sudo vi /etc/selinux/config

SELINUX=disabled

停止防火墙

sudo systemctl disable firewalld
sudo systemctl stop firewalld

更新系统

sudo yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
sudo yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
sudo yum install yum-utils
sudo yum-config-manager --enable remi-php72
sudo yum update
sudo yum install php-gd php-pdo php-opcache php-fpm php-pecl-redis php-pecl-mysql php-pecl-mysql php-mbstring php-intl php-cli php-xml
sudo yum install nginx -y
sudo yum install mariadb mariadb-server -y

修改nginx配置

mkdir /data/log/nginx /data/run/nginx -p
sudo service nginx stop 
sudo vim /etc/nginx/nginx.conf

nginx.conf配置修改如下:

user vagrant;
error_log /data/log/nginx/error.log;
pid /data/run/nginx/nginx.pid;

access_log  /data/log/nginx/access.log  main;

include /data/phpcode/projectname/vagrant/nginx/app.conf;

测试nginx配置

sudo nginx -t

修改nginx service配置:

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

nginx.service修改内容如下:

[Service]
PIDFile=/data/run/nginx/nginx.pid

重新加载service

sudo systemctl daemon-reload
sudo systemctl start nginx

修改PHP配置

mkdir  /data/run/php-fpm/session /data/run/php-fpm/wsdlcache /data/run/php-fpm/opcache /data/log/php-fpm/ -p
sudo service php-fpm stop
sudo vim /etc/php-fpm.d/www.conf

配置内容

user = vagrant
group = vagrant
php_value[session.save_path]    = /data/run/php-fpm/session
php_value[soap.wsdl_cache_dir]  = /data/run/php-fpm/wsdlcache
php_value[opcache.file_cache]  = /data/run/php-fpm/opcache
php_admin_value[error_log] = /data/log/php-fpm/www-error.log
slowlog = /data/log/php-fpm/www-slow.log
request_slowlog_timeout = 1

重启

sudo service php-fpm stop

配置Mysql

mkdir /data/mysql /data/run/mariadb /data/log/mariadb -p
sudo service mariadb stop
sudo vim /etc/my.cnf

mysqld配置

[mysqld]
datadir=/data/mysql
socket=/usr/lib/mysql/mysql.sock

[mysqld_safe]
log-error=/data/log/mariadb/mariadb.log
pid-file=/data/run/mariadb/mariadb.pid

mysql client 配置

[client]

初始化数据库

sudo /usr/libexec/mariadb-prepare-db-dir mariadb.service

修改systemd配置

sudo vim /usr/lib/systemd/system/mariadb.service

配置内容

User=vagrant
Group=vagrant

重载systemd

sudo systemctl daemon-reload
sudo systemctl start mariadb

修改mysql账号密码

'/usr/bin/mysqladmin' -u root password 'new-password'
'/usr/bin/mysqladmin' -u root -h localhost.localdomain password 'new-password'

# Alternatively you can run:
'/usr/bin/mysql_secure_installation'

配置composer

下载安装文件

php -r "copy('https://install.phpcomposer.com/installer', 'composer-setup.php');"

安装

php composer-setup.php

删除安装文件

php -r "unlink('composer-setup.php');"

设置全局路径(windows请按参考文档设置)

sudo mv composer.phar /usr/bin/composer

配置使用国内镜像

composer config -g repo.packagist composer https://packagist.phpcomposer.com

linux下安装usb无线网卡驱动

查看显卡型号

lsusb 

可以看到显卡型号为RTL8188EUS。通过搜索查找到https://github.com/lwfinger/rtl8188eu这个驱动。

安装驱动

wget https://github.com/lwfinger/rtl8188eu/archive/master.zip
unzip master.zip
cd rtl8188eu-master
make all
sudo make install

自动编译

sudo dkms add ./rtl8188eu
sudo dkms build 8188eu/1.0
sudo dkms install 8188eu/1.0

fedora 26快速安装PHP开发环境

Nvidia显卡驱动

sudo dnf config-manager --add-repo=http://negativo17.org/repos/fedora-nvidia.repo  
sudo dnf install kernel-devel dkms-nvidia -y

Vim & git & hg & svn

sudo dnf install vim git hg subversion -y

sudo不需要密码

sudo visudo

%wheel  ALL=(ALL)       NOPASSWD: ALL

禁止selinux

sudo setenforce 0 

sudo vi /etc/selinux/config

SELINUX=disabled

右键打开控制台窗口

sudo dnf install nautilus-open-terminal -y

Development tools

sudo dnf groupinstall "Development tools" -y

nginx & php & mysql

sudo dnf install  nginx php-cli php-devel php-mbstring php-opcache php-mysqlnd php-intl php-mcrypt php-pdo php-xml php-pecl-memcache  php-pecl-redis php-pecl-sphinx php-pecl-zip php-pgsql php-xdebug php-gd  php-pecl-redis  php-pecl-imagick  php-fpm -y
sudo dnf install mariadb mariadb-server -y
sudo systemctl enable php-fpm nginx mariadb

修改nginx配置

sudo service nginx stop 
sudo vim /etc/nginx/nginx.conf

nginx.conf配置修改如下:

user ning;
error_log /data/log/nginx/error.log;
pid /data/run/nginx.pid;

access_log  /data/log/nginx/access.log  main;

include /data/phpcode/projectname/vagrant/nginx/app.conf;

测试nginx配置

sudo nginx -t

修改nginx service配置:

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

nginx.service修改内容如下:

[Service]
PIDFile=/data/run/nginx.pid
ExecStartPre=/usr/bin/rm -f /data/run/nginx.pid

重新加载service

sudo systemctl daemon-reload
sudo systemctl start nginx

修改PHP配置

sudo service php-fpm stop
sudo vim /etc/php-fpm.d/www.conf

配置内容

user = ning
group = ning
listen = 127.0.0.1:9000
listen.acl_users = ning
php_value[session.save_path]    = /log/php/session
php_value[soap.wsdl_cache_dir]  = /log/php/wsdlcache
;php_value[opcache.file_cache]  = /log/php/opcache

重启

sudo service php-fpm stop

配置composer

下载安装文件

php -r "copy('https://install.phpcomposer.com/installer', 'composer-setup.php');"

安装

php composer-setup.php

删除安装文件

php -r "unlink('composer-setup.php');"

设置全局路径(windows请按参考文档设置)

sudo mv composer.phar ~/bin/composer

配置使用国内镜像

composer config -g repo.packagist composer https://packagist.phpcomposer.com